【翻译十九】-java之执行器
Executors
In all of the previous examples, there's a close connection between the task being done by a new thread, as defined by its Runnable object, and the thread itself, as defined by aThread object. This works well for small applications, but in large-scale applications, it makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors. The following subsections describe executors in detail.
- Executor Interfaces define the three executor object types.
- Thread Pools are the most common kind of executor implementation.
- Fork/Join is a framework (new in JDK 7) for taking advantage of multiple processors.
译文:
执行器
在所有以前的实例中,我们对任务使用新线程有一个近距离的接触。作为Runnable对象定义的线程,和线程,通过一个Thread对象定义的。这对小型程序工作的很好,但是对大型程序,对于线程与其他程序分开管理是有必要的。封转了这些函数的对象是executors.接下来的章节详细的描述了executors。
- 执行接口 定义了三种执行对象类型
- 线程池 是最常见的执行实现。
- 分开与合并是一个框架(在JDK7新加的)对于多处理器非常有用。
Executor Interfaces
The java.util.concurrent package defines three executor interfaces:
Executor, a simple interface that supports launching new tasks.ExecutorService, a subinterface ofExecutor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.ScheduledExecutorService, a subinterface ofExecutorService, supports future and/or periodic execution of tasks.
Typically, variables that refer to executor objects are declared as one of these three interface types, not with an executor class type.
The Executor Interface
The Executor interface provides a single method, execute, designed to be a drop-in replacement for a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace
(new Thread(r)).start();
with
e.execute(r);
However, the definition of execute is less specific. The low-level idiom creates a new thread and launches it immediately. Depending on the Executor implementation, execute may do the same thing, but is more likely to use an existing worker thread to run r, or to place r in a queue to wait for a worker thread to become available. (We'll describe worker threads in the section on Thread Pools.)
The executor implementations in java.util.concurrent are designed to make full use of the more advanced ExecutorService andScheduledExecutorService interfaces, although they also work with the base Executor interface.
The ExecutorService Interface
The ExecutorService interface supplements execute with a similar, but more versatile submit method. Like execute, submitaccepts Runnable objects, but also accepts Callable objects, which allow the task to return a value. The submit method returns a Future object, which is used to retrieve the Callable return value and to manage the status of both Callable andRunnable tasks.
ExecutorService also provides methods for submitting large collections of Callable objects. Finally, ExecutorService provides a number of methods for managing the shutdown of the executor. To support immediate shutdown, tasks should handleinterrupts correctly.
The ScheduledExecutorService Interface
The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes aRunnable or Callable task after a specified delay. In addition, the interface defines scheduleAtFixedRate andscheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals.
译文:
执行接口
java.util.concurrent包定义了三种执行接口:
- 执行器,一种简单的接口支持载入新任务。
- 执行服务,执行器的一个子接口,它增加了管理生命周期的特性,包括私有的任务和执行器本身。
- 调度执行服务,执行服务的一个子接口,支持将来或者周期执行的任务。
代表性的,可执行的对象变量被定义为这三种类型的一种,而不是一个可执行类的类型。执行器接口Executor接口提供了一个简单的方法,执行,定义了一种通用的创建线程的规则。如果r是一个Runnable对象,e是一个执行器对象,你可使用如下的方法替代。
(new Thread(r)).start();
替代为:e.execute(r);
然而,execute的定义是没有那么严格的。低级的创建线程的规则是创建之后马上载入它。依赖于实现Executor接口,execute可以实现相同的事情。但是跟喜欢用一个已经催在的工作线程来运行它,或者在一个队列中替代r来等待工作线程变成可用的。(我们将会在线程池这一节描述工作线程)
在java.util.concurrent中是想的执行器被定义为更加充分的使用ExecutorService和ScheduleExecutorService接口。尽管他们都是基于Executor接口的。
执行服务接口
执行服务接口补充了执行器,但是更多的通用的提交方法。像执行器,提交方法接受了Runable对象,但是也接受额Callable对象,它语序任务返回一个值。submit方法返回一个Future对象,它被用来重新检索Callable返回值和管理Callable和Runnable任务的状态。
执行服务也提供了一种方法提交Callable对象的大型集合。ExecutorService提供了许多方法管理关闭执行器。为了支持立即提交,任务需要实现正确实现interrupts方法。
调度执行服务接口
调度服务接口提供了基于它的父类执行服务的许多调度方法,可以执行Runnable或者Callable任务在指定的一个延迟之后。进一步,这个接口定义了schedulAtFixedRate和scheduleWithFixDelay,支持重复执行指定的任务,在一个定义的间隔内。
【翻译十九】-java之执行器的更多相关文章
- Java进阶(三十九)Java集合类的排序,查找,替换操作
Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...
- 菜鸡的Java笔记 第十九 - java 继承
继承性的主要目的,继承的实现,继承的限制 继承是面向对象中的第二大主要特点,其核心的本质在于:可以将父类的功能一直沿用下去 为什么需要继承? ...
- Java学习笔记十九:Java中的访问控制修饰符
Java中的访问控制修饰符 一:Java修饰符的种类: 访问修饰符 非访问修饰符 修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: public class Hello ...
- Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器
Writing Surface Shaders Writing shaders that interact with lighting is complex. There are different ...
- 菜鸡的Java笔记 第二十九 - java 单例设计模式
SingleCase 单例设计模式 1.单例设计模式的特点 2.多例设计模式的特点 内容 单例设计模式 现在如果说有这么一个程序类 class S ...
- Gradle 1.12翻译——第十九章. Gradle 守护进程
有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...
- Gradle 1.12用户指南翻译——第二十九章. Checkstyle 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- Gradle 1.12用户指南翻译——第三十九章. IDEA 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- Gradle 1.12用户指南翻译——第四十九章. Build Dashboard 插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
随机推荐
- static小结
1.隐藏:编译多个文件时,所有未加static的全局变量.全局函数都具有全局可见性. 如果加了static,就会对其他源文件隐藏,利用这一特性可以在不同文件中定义相同的 变量名或函数名,而不用担心冲突 ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- Laplacian matrix 从拉普拉斯矩阵到谱聚类
谱聚类步骤 第一步:数据准备,生成图的邻接矩阵: 第二步:归一化普拉斯矩阵: 第三步:生成最小的k个特征值和对应的特征向量: 第四步:将特征向量kmeans聚类(少量的特征向量):
- [黑科技]bit reverse
写FFT的时候yy出来了这个bit reverse...时间复杂度O(n),常数大概是(a[x>>1]>>1)|((x&1)<<26)的二分之一(-O3下) ...
- java servlet的工作原理
servlet本质上就是java类嘛.不过是有特殊规范的java类而已.下面就说一说为什么servlet要有特殊规范. 首先,考虑一下什么地方用servlet,WEB应用,而且是需要servlet容器 ...
- springMVC 访问404
问题:404 但是其他的controller可以访问!!!
- PHP json_encode / json_decode
2015年3月26日 14:14:16 PHP的json函数对几个特殊值的处理笔记 <?php //----------编码 $a = array(); $b = json_encode($a) ...
- WPS Office文档未保存怎么恢复
有时候用WPS Office时,文档还没保存,因为电脑卡死或者关机,再次打开时编辑的内容都不见了,这个时候可以利用WPS自带的备份功能来恢复文档,表格.幻灯片.文档都是可以的. 首先单击WPS左上角的 ...
- ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)
做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...
- glib-2.49.4-msys-x86-staticLib.7z
glib-2.49.4 MSYS 静态库 编译 export LIBFFI_CFLAGS=" -I/usr/local/lib/libffi-3.2.1/include " \ e ...