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 ...
随机推荐
- ELK+FileBeat+Log4Net搭建日志系统
ELK+FileBeat+Log4Net搭建日志系统 来源:https://www.zybuluo.com/muyanfeixiang/note/608470 标签(空格分隔): ELK Log4Ne ...
- python内置模块(3)
博主所有python文章均为python3.5.1环境. 目录: 1.collections 2.subprocess 3.configparser 4.XML 5.shutil 一. collect ...
- 使用MinGW 编译 iconv 库
原文链接: http://www.code-by.org/viewtopic.php?f=54&t=166 GNU页面 http://ftp.gnu.org/pub/gnu/libiconv/ ...
- ASP.NET MVC传送参数至服务端
ASP.NET MVC传送参数至服务端,前端与服务端的写法,你可以参考与采用适合你的需求的.当你只传递一两个参数也许觉得没有什么,如果一个方法中带的参数多的话,可以考虑model,前端可以考虑对象进行 ...
- [CLR via C#]16. 数组
数组是允许将多个数据项当作一个集合来处理的机制.CLR支持一维数组.多维数组和交错数据(即由数组构成的数组).所有数组类型都隐式地从System.Array抽象类派生,后者又派生自System.Obj ...
- jquery实现网页选项卡
这个功能在现在的网站中使用较为普遍,就是以选项卡的形式来对一些内容做了分类.,比如下面的天猫商城. 下面的源码是仿照天猫写的一个选项卡,实现起来的效果如下. 主要是利用我们在点击相应板块是触发它的单击 ...
- Java、Hibernate(JPA)注解大全
1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...
- 小白学Linux--虚拟机下安装Ubuntu16
最近接收到任务,说是下半年可能要搞全文检索.听到后顿时炸锅了,一方面是对新技术的兴奋(当然主要还是这技术比较值钱),另一方面,我TM连Linux都不会玩,怎么搞全文检索.怀揣着对开源世界的无线向往(恐 ...
- [PE结构分析] 8.输入表结构和输入地址表(IAT)
在 PE文件头的 IMAGE_OPTIONAL_HEADER 结构中的 DataDirectory(数据目录表) 的第二个成员就是指向输入表的.每个被链接进来的 DLL文件都分别对应一个 IMAGE_ ...
- Laravel Predis Error while reading line from the server.
问题 Laravel说明文档中的 Redis 发布与订阅案例,命令行运行php artisan redis:subscribe 到60s自动断开并报错 [Predis\Connection\Conne ...