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 ...
随机推荐
- elasticsearch配置
配置文件详解1.0版 配置文件位于es根目录的config目录下面,有elasticsearch.yml和logging.yml两个配置,主配置文件是elasticsearch.yml,日志配置文件是 ...
- DataSet导出到Excel,并生成文件(C#实现,可合并行和列)
using System; using System.IO; using System.Data; using System.Reflection; using System.Diagnostics; ...
- IIS理解
WEB开发基础 1IIS原理 IIS的本质其实就是一个sorket的服务器,浏览器就是一个sorket的客户端,浏览器发送请求信息给IIS,IIS返回信息给浏览器显示,就这么简单. 1http.sys ...
- Apns推送中的的json格式介绍
在开发向苹果Apns推送消息服务功能,我们需要根据Apns接受的数据格式进行推送.下面接受我在进行apns推送时候总结的一点apns服务接受的Json数据格式 示例 1: 以下负载包含哦一个简单的 a ...
- js限制input输入
1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true <input type="submit" value=" ...
- PHP Warning: Module 'modulename' already loaded in Unknown on line 0
问题 当在命令行运行PHP的CLI版本时,您可能会收到类似以下错误: [root@myserver /root]$ php -v PHP Warning: Module 'pcre' already ...
- python peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.
最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...
- 「C语言」C输出hello world!系统发生了什么?
本篇文章全部摘抄自学长博客供以后学习: http://efraim.me/2015/12/05/tech-linux-2015-12-05/ 排版因与博客园编辑器不同而稍作修改. 输出hello wo ...
- winform(无边框窗体与timer)
一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要放置的控件可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入(pictureBox1_MouseE ...
- .net aes加密视频等文件
公司学习平台在app端下载下来的视频需要加密 随查找资料参考一些写法 写了aes的加密方法 记录防止忘记 using System; using System.Collections.Generic; ...