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的更多相关文章

  1. 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 ...

  2. Effective Java 53 Prefer interfaces to reflection

    Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...

  3. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  4. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  5. Effective Java 20 Prefer class hierarchies to tagged classes

    Disadvantage of tagged classes 1. Verbose (each instance has unnecessary irrelevant fields). 2. Erro ...

  6. Effective Java 25 Prefer lists to arrays

    Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Java基础——事务

    一.事务 简单点说,事务就是一件事情.所有与事务相关的内容都是围绕这一件事情展开的. 二.事务的特性:ACID A:Atomicity(原子性),事务必须是一个不可分割的整体. C:Consisten ...

  2. 使用SignalR构建一个最基本的web聊天室

    What is SignalR ASP.NET SignalR is a new library for ASP.NET developers that simplifies the process ...

  3. ADO.NET ExcuteReader复习

    private void Button_Click(object sender, RoutedEventArgs e) { //ADO.NET 连接方式查询数据库 ExcuteReader执行查询 / ...

  4. javaScript一些函数--Math()

    1.不能显式地创建一个Math对象,直接使用它就可以了: 2.Math对象不能存储数据,和String,Date对象不同: 3.前面知道了parseInt()函数会通过消去小数点后面的一切,来使一个小 ...

  5. Water --- CSU 1550: Simple String

    Simple String Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1550 Mean: 略. analy ...

  6. 网狐6603棋牌游戏源码.rar

    网狐6603棋牌游戏源码.rar http://pan.baidu.com/s/1dFgGNq5 网络收集仅供学习,下载后请务必在24小时内删除! 以上是原vc6.0源码,下载后使用vs2015编译, ...

  7. Python入门笔记(12):表达式和语句

    一.Python语句 if语句.else语句.elif语句.条件表达式.while语句.for语句.break语句.continue语句.pass语句.Iterators(迭代器).列表解析 二.常用 ...

  8. 小巧方便的MVC后端验证码,供大家学习借鉴

    调用: public ActionResult Vcode()//验证码 { string code = ValidateCode.CreateRandomCode(4); ValidateCode. ...

  9. wpf 查找页面的所有TextBox

    private void EnumVisual(Visual myVisual) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount( ...

  10. CentOS修改服务器系统时间

    linux安装完毕后,一般都是国外的世界,一点都不方便设置任务,或者导致网站获取本地的时间错乱,所以就需要把服务器的时间改为和本地时间一致,也就是换成中国的时间. 第一条指令:date –s '201 ...