【翻译十九】-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
, submit
accepts 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 ...
随机推荐
- GPS坐标互转:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图)
WGS-84:是国际标准,GPS坐标(Google Earth使用.或者GPS模块)GCJ-02:中国坐标偏移标准,Google Map.高德.腾讯使用BD-09:百度坐标偏移标准,Baidu Map ...
- time()函数,dirname(__FILE__) 的使用总结
time()函数将返回从1970-1-1 0:0:0到当前时间的秒数.(整型) dirname(__FILE__) php中定义了一个很有用的常数,即 __file__ 这个内定常数是当前php程序的 ...
- 初识hibernate小案例
使用hibernate前需要导入相关JAR包. 1.它可以接受词文法语言描述,并能产生识别这些语言的语句的程序 2.是一个Java的XML API,类似于jdom,用来读写XML文件的 3.支持注解配 ...
- python wraps
用代码说明问题: def d(f): def _d(*args, **kwargs): print f.__name__, ' is called' f(*args, **kwargs) return ...
- javascript中apply、call和bind的区别,容量理解,值得转!
a) javascript中apply.call和bind的区别:http://www.cnblogs.com/cosiray/p/4512969.html b) 深入浅出 妙用Javascrip ...
- Robot Framework + Selenium2Library环境下,结合Selenium Grid实施分布式自动化测试
最近一段时间,公司在推行自动化测试流程,本人有幸参与了自定义通用控件的关键字封装和脚本辅助编写.数据驱动管理.测试用例执行管理等一系列工具软件的研发工作,积累了一些经验,在此与大家做一下分享,也算是做 ...
- Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序
在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...
- Js注释
注释 介绍 作用 合作分享:方便他人阅读,便于分享 沉淀总结:容易忘记代码,自己总结沉淀 形式 1.// 双斜杠 2./**/斜杠星号 常用标签 标签 描述 @module 标明当前文件模块,在这个文 ...
- Linux rpm 命令参数使用详解[介绍和应用]
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两 ...
- 【剑指offer】题目36 数组中的逆序对
数组中任取两个数字,如果前面的数字大于后面的数字称为一个逆序对 如:1,2,1,2,1 有3个逆序对 思路:知道O(N2)肯定是错的.开始想hash,试图找到O(n)的算法,想了很久,找不到.后来想到 ...