【翻译十九】-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 ...
随机推荐
- UML统一建模编程
PowerDesigner 可以通过类图直接可视化生成代码 UML模型元素: 表示模型中的某个概念(类.对象.用例.结点.组件.包.接口等等): 表示模型间相互连接的关系(关联.泛化.依赖.聚集).
- 8.eclipse调试smali
一.重打开包APK 1.apktool解包文件 apktool d -d XXX.apk 这里注意使用-d参数,生成的smali文件才是以java结尾的,才能被eclipse识别 2.找到Androi ...
- poj 1521
http://poj.org/problem?id=1521 题意:给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度 ...
- 用Javascript主动更行URL
参考---ttp://www.oschina.net/translate/manipulating-url-using-javascript-without-freshing-the-page var ...
- ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))
通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...
- NoSQL之【MongoDB】学习(二):DML和查询操作说明
摘要: 操作MongoDB的方法和关系型数据库差别很大,现在对他们进行说明,后期会逐步完善. ##开头表示MySQL** 开头表示MongoDB 创建: Mongodb:文档数据库,擅长存非结构化数据 ...
- Divide and Conquer:River Hopscotch(POJ 3258)
去掉石头 题目大意:一群牛在河上的石头上跳来跳去,现在问你如何通过去掉M个石头,使得牛跳过石头的最短距离变得最大? 这一题比较经典,分治法的经典,二分法可以很方便处理这个问题,我们只要明白比较函数这 ...
- CSS3 -web-box-shadow实现阴影效果
-webkit-box-shadow:2px -2px 10px #06c; 给元素添加阴影效果 text-shadow 是给文本添加阴影效果属性同上 形成的阴影跟阴影本体大小一致,四个属性分别代表 ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【hadoop2.6.0】通过代码运行程序流程
之前跑了一下hadoop里面自带的例子,现在顺一下如何通过源代码来运行程序. 我懒得装eclipse,就全部用命令行了. 整体参考官网上的:http://hadoop.apache.org/docs/ ...