Effective Java 68 Prefer executors and tasks to threads
Principle
The general mechanism for executing tasks is the executor service. If you think in terms of tasks and let an executor service execute them for you, you gain great flexibility in terms of selecting appropriate execution policies. In essence, the Executor Framework does for execution what the Collections Framework did for aggregation.
Exceutor Framework is a flexible interface-based task execution facility.
Creating work queue with Executor
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(runnable);
Here is how to tell the executor to terminate gracefully (if you fail to do this, it is likely that your VM will not exit):
executor.shutdown();
Usage of executor service
|
Usage |
Utility |
|
Wait for a particular task to complete. |
"background thread SetObserver" |
|
Wait for any or all of a collection of tasks to complete. |
invokeAny or invokeAll methods |
|
Wait for the executor service's graceful termination to complete. |
awaitTermination method |
|
Retrive the results of tasks one by one as they complete. |
ExecutorCompletionService |
|
Control every aspect of a thread pool's operation |
ThreadPoolExecutor class |
|
Small program or lightly loaded server (submitted tasks are not queued but immediately handed off to a thread for execution. If no threads are available, a new one is created. ) |
Executors.newCachedThreadPool |
|
Heavily loaded production server |
Executors.newFixedThreadPool ThreadPoolExecutor |
Thread pool (Fixed or variable service number of threads) .
Task - The key abstraction is the unit of work.
|
Task category |
return value |
|
Runnable |
N |
|
callable |
Y |
|
Multithread |
Time accuracy for long running tasks |
Grace recover on uncaught exception |
|
|
java.util.Timer |
N |
N |
N |
|
ScheduledThreadPoolExecutor |
Y |
Y |
Y |
Effective Java 68 Prefer executors and tasks to threads的更多相关文章
- Effective Java 69 Prefer concurrency utilities to wait and notify
Principle Use the higher-level concurrency utilities instead of wait and notify for easiness. Use Co ...
- Effective Java 53 Prefer interfaces to reflection
Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...
- Effective Java 35 Prefer annotations to naming patterns
Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...
- Effective Java 18 Prefer interfaces to abstract classes
Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...
- Effective Java 20 Prefer class hierarchies to tagged classes
Disadvantage of tagged classes 1. Verbose (each instance has unnecessary irrelevant fields). 2. Erro ...
- Effective Java 25 Prefer lists to arrays
Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...
- Effective Java 46 Prefer for-each loops to traditional for loops
Prior to release 1.5, this was the preferred idiom for iterating over a collection: // No longer the ...
- Effective Java 49 Prefer primitive types to boxed primitives
No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
随机推荐
- Sprint第三个冲刺(第一天)
一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:
- $.ajax、$.post、from表单序列化工具
$.ajax\$.post <script type="text/javascript" language="javascript" src=" ...
- CheckListBox的实现方式分析
实际项目中常常要实现有CheckBox列表框.但是WPF没有自带这样的一个控件,下面就用Style来实现这样的功能.而对于CheckBox列表框,又常常会有一个Select All的CheckBox来 ...
- Python入门笔记(17):错误、异常
一.什么是错误,什么是异常,它们两者区别 这里解释如下:个人觉得很通俗易懂 错误是指在执行代码过程中发生的事件,它中断或干扰代码的正常流程并创建异常对象.当错误中断流程时,该程序将尝试寻找异常处理程序 ...
- 数据库一次性插入10w条数据,怎么插入效率快
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题 下面介绍SQL Server支持的两种批量 ...
- DataList分页访问FooterTemplate模板里的控件
今天做DataList分页的时候,突然想把分页控件写在FooterTemplate模板里面,弄了很久都访问不到控件,终于发现问题所在,以下是访问FooterTemplate里控件的方法: <Fo ...
- 在Android设备上判断设备是否支持摄像头
private boolean hasCamera(){ boolean hasCamera=false; PackageManager pm=getActivity().getPackageMana ...
- Java与线程
导语 我们知道,new一个thread,调用它的start的方法,就可以创建一个线程,并且启动该线程,然后执行该线程需要执行的业务逻辑, 那么run方法是怎么被执行的呢? Java线程和os线程 os ...
- Asp.net发布的CheckList
Asp.net Web 应用程序正式发布前,我们还是做一些检查,所以需要这个CheckList,如下图今天的Asp.net 已演化这样的了: 但不管是什么组件,目前的Web最终还得通过H ...
- [翻译]:SQL死锁-锁与事务级别
其实这一篇呢与解决我项目中遇到的问题也是必不可少的.上一篇讲到了各种锁之间的兼容性,里面有一项就是共享锁会引起死锁,如何避免呢,将我们的查询都设置中read uncommitted是否可行呢?其结果显 ...