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 ...
随机推荐
- 禁用mac Command w
事情是这样的:历经各种调查,终于定位到了一个bug,正在我准确Command + 数字 切换下iterm2的窗口时,让我懵逼的事情发生了,我们终端不见了,对不见了.心里万马奔腾啊.什么鬼?原来自己误按 ...
- 初涉SQL Server性能问题(1/4):服务器概况
当你作为DBA时,很多人会向你抱怨:“这个程序数据加载和蜗牛一样,你看看是不是服务器出问题了?”造成这个问题的原因有很多.可能是程序应用服务器问题,网络问题,程序实现方式问题,数据库服务器负荷过重.不 ...
- Android开发切换host应用
由于在工作过程中常需要切换手机的host来测试不同服务器上的接口,所以想到需要这么个软件. SwitchHost在PC上是一款很好用的修改Host的软件,手机上也需要这么一款App(当然手机需要已经R ...
- 关于JQUERY操作XML问题!
使用JQUERY操作XML方法: 1.$.get(”xml文件路径",function(data){}); 2.$.Post(”xml文件路径",function(data){}) ...
- MyBatis魔法堂:ResultMap详解
一.前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了, ...
- SerializationUtility
public static T LoadFromXml<T>(string fileName) { FileStream fs = null; try { XmlSerializer se ...
- Linux永久修改系统时间和时区方法
修改时区: 1> 找到相应的时区文件 /usr/share/zoneinfo/Asia/Shanghai 用这个文件替换当前的/etc/localtime文件. 或者找你认为是标准时间的服务器, ...
- (C#)WinForm窗体间传值
1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int v ...
- MySQL主从复制与读写分离 --非原创
原文出处:http://www.cnblogs.com/luckcs/articles/2543607.html MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 ...
- javascript运算符的优先级
最基木的运算符优先级就是所谓的“先乘除,后加减”.对于优先顺序处于同一层次上的运算符,按照从左到右出现的顺序计算.下面给出javascript定义的所有运算符的优先级.运算符 优先顺序成员选择.括号. ...