【翻译十九】-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 ...
随机推荐
- 项目:DoubleFaceCamera
代码在github: https://github.com/Viyu/DoubleFacedCamera 这个app的想法是:用手机的前置和后置摄像头同时拍摄画面合成输出一张图,把拍摄者和被拍摄者的画 ...
- HTML之文本框关键字显示
文本框默认显示 "请输入关键字",当鼠标点击输入框的时候, "请输入关键字"这几个字消失,移出文本框又显示出来 <!DOCTYPE html> &l ...
- 解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now
先查看文件真正的属性是什么? [root@xxxxx ~]# tar -zxvf tcl8.4.16-src.tar.gz gzip: stdin: not in gzip format tar: ...
- td的title属性
今天才知道html元素td还有一个title属性,就是一个tooltip的东西,即当你把鼠标放在td上面的时候,会弹出一个提示语,这个提示语就是td的title. 把td的文本赋值给title: ht ...
- 2017/1/7 学习笔记 jar包,maven
① 关于tar,jar,war文件 tar是通用的另一种打包格式,为了部署到服务器时方便. jar是java app server识别的java部署格式,其实是Zip文件,只是内部的文件有规范. wa ...
- C#的is和as操作符来进行强制类型转换&&值类型的拆箱、装箱
if(o is Employee) { Employee e=(Employee)o; //在if语句剩余的部分中使用e; } Employee e=o as Employee; if(e!=null ...
- poj 1094(拓扑排序)
http://poj.org/problem?id=1094 题意:给你m个字母,有n个判断语句.求在哪个语句就可以判断出这个是不是一个环,或者在哪个语句可以判断出这些字母的排序规则,或者就是不能确定 ...
- Python之异常追踪模块:traceback
正常时输出追踪信息: import traceback def stack(): print 'The python stack:' traceback.print_stack() from twis ...
- yum安装所需要的开发库
yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel openssl-devel n ...
- 如何限制一个类只在堆上分配和栈上分配(StackOnly HeapOnly)
[本文链接] http://www.cnblogs.com/hellogiser/p/stackonly-heaponly.html [题目] 如何限制一个类只在堆上分配和栈上分配? [代码] C+ ...