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 ...
随机推荐
- ASP.NET MVC权限验证 封装类
写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...
- jQuery--捕获键盘敲击
功能 当页面加载完成后,用户键盘按下某个键后,jQuery能够捕获到一个数字,从而执行一系列动作. 格式 1 2 3 4 5 6 7 8 9 10 11 12 $(function(){ $ ...
- MVC增删查改,从数据库到后台,到前端,整个复习一下
就当是记笔记吧,这里,就不讲什么版式了,首先上数据库脚本,这个是我这次练习用到的数据库脚本: USE [DB_USERS] GO /****** Object: Table [dbo].[Studen ...
- Linq专题列表
什么是Linq? Linq(Language-Integrated Query),即语言集成查询.是微软的一项新技术,能够将查询功能直接引入.NET Framework3.5 所支持的编程语言(C#, ...
- Asp.NET MVC JSON序列化问题
最近在做项目的时候遇到一个JSON序列化问题. 环境:ASP.NET MVC 4.0 数据库:SQL 2008 在将获取的数据从后台以json的形式传给前台的easyui treegrid绑定的时候通 ...
- 创建一个带模版的用户控件 V.3
再重构此篇<创建一个带模版的用户控件 V.2>http://www.cnblogs.com/insus/p/4164149.html 让其它动态实现header,Item和Footer. ...
- 如何用c语言调用c++做成的动态链接库
今天在做东西的时候遇到一个问题,就是如何在C语言中调用C++做的动态链接库so文件如果你有一个c++做的动态链接库.so文件,而你只有一些相关类的声明, 那么你如何用c调用呢,别着急,本文通过一个小小 ...
- 【百度SEO优化】如何让蜘蛛爬行你的网站
大家都知道,现在做网站简单,但是推广就比较困难了,可能一些商家引入投资,直接烧钱做广告来推广,但是对于一些小站长,是没有那么多资金的.因此我们就要懂得一些SEO优化的知识了,简单介绍一下: 怎么让百度 ...
- sql server聚合函数sum计算出来为空,怎样返回0
通常我们计算数据库中表的数据有几个常用的聚合函数 1.count : 计数 2.sum: 计算总和 3.avg: 取平均值 4.max: 取最大值 5.min: 取最小值 6.isnull: 当返回数 ...
- 机器学习实战 - 读书笔记(13) - 利用PCA来简化数据
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第13章 - 利用PCA来简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. ...