JDK1.5 Excutor 与ThreadFactory
Excutor 源码解读: /**
* An object that executes submitted {@link Runnable} tasks. This
* interface provides a way of decoupling task submission from the
* mechanics of how each task will be run, including details of thread
* use, scheduling, etc. An {@code Executor} is normally used
* instead of explicitly creating threads. For example, rather than
* invoking {@code new Thread(new(RunnableTask())).start()} for each
* of a set of tasks, you might use:
*
Excutor 是一个可以执行提交过来实现Runnable 接口的对象,这个接口提供了一种将任务的提交以及
这个任务如何去执行进行了解耦; 包含了线程线程的使用细节,调度等;
我们通常使用Excutor 而不是显式的创建线程,比如说,我们不会调用对每一个任务进行new Thread (Runnabe ()).start()
方法;而是使用如下:
* <pre>
* Executor executor = <em>anExecutor</em>;
* executor.execute(new RunnableTask1());
* executor.execute(new RunnableTask2());
* ...
* </pre>
创建一个执行器Excutor
调用执行器的execte方法
* However, the {@code Executor} interface does not strictly
* require that execution be asynchronous. In the simplest case, an
* executor can run the submitted task immediately in the caller's
* thread:
然而,Excutor执行器并不是严格的要求执行是异步的,一个简单的情况,
一个执行器在调用者线程中会立刻运行提交的任务,不理解看下面例子:
* <pre> {@code
* class DirectExecutor implements Executor {
* public void execute(Runnable r) {
* r.run();
* }
* }}</pre>
运行了.run 方法,并不会启用一个新的线程,.start 才会启动一个新的线程,就是这个意思
* More typically, tasks are executed in some thread other
* than the caller's thread. The executor below spawns a new thread
* for each task.
*
* <pre> {@code
* class ThreadPerTaskExecutor implements Executor {
* public void execute(Runnable r) {
* new Thread(r).start();
* }
* }}</pre>
*
* Many {@code Executor} implementations impose some sort of
* limitation on how and when tasks are scheduled. The executor below
* serializes the submission of tasks to a second executor,
* illustrating a composite executor.
*
* <pre> {@code
* class SerialExecutor implements Executor {
* final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
* final Executor executor;
* Runnable active;
*
* SerialExecutor(Executor executor) {
* this.executor = executor;
* }
*
* public synchronized void execute(final Runnable r) {
* tasks.offer(new Runnable() {
* public void run() {
* try {
* r.run();
* } finally {
* scheduleNext();
* }
* }
* });
* if (active == null) {
* scheduleNext();
* }
* }
*
* protected synchronized void scheduleNext() {
* if ((active = tasks.poll()) != null) {
* executor.execute(active);
* }
* }
* }}</pre>
*
* The {@code Executor} implementations provided in this package
* implement {@link ExecutorService}, which is a more extensive
* interface. The {@link ThreadPoolExecutor} class provides an
* extensible thread pool implementation. The {@link Executors} class
* provides convenient factory methods for these Executors.
************很重要了***************
在这个package 中Excutor 的实现提供了 关于Excutor 接口的扩展ExcutorService
ThreadPoolExcutor 类提供了可扩展的线程池的实现, Excutors 提供了 方便的工厂方法(常用)
* <p>Memory consistency effects: Actions in a thread prior to
* submitting a {@code Runnable} object to an {@code Executor}
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
* its execution begins, perhaps in another thread.
*
ThreadFactory 源码解读“”
/**
* An object that creates new threads on demand. Using thread factories
* removes hardwiring of calls to {@link Thread#Thread(Runnable) new Thread},
* enabling applications to use special thread subclasses, priorities, etc.
ThreadFactory 是一个按需要创建线程的对象,使用线程工厂消除了使用
Thread(Runnable) new Thread 创建线程的硬连接 能够使应用程序使用使用特殊的线程子类,优先级等;下面是一个简单的例子:
* <p> * The simplest implementation of this interface is just: * <pre>
{@code * class SimpleThreadFactory implements ThreadFactory
{ * public Thread newThread(Runnable r) {
* return new Thread(r); * } * }}
</pre>
JDK1.5 Excutor 与ThreadFactory的更多相关文章
- java并发程序——Excutor
概述 Excutor这个接口用的不多,但是ThreadPoolExcutor这个就用的比较多了,ThreadPoolExcutor是Excutor的一个实现.Excutor体系难点没有,大部分的关键点 ...
- 线程池ThreadPoolExecutor源码解读研究(JDK1.8)
一.什么是线程池 为什么要使用线程池?在多线程并发开发中,线程的数量较多,且每个线程执行一定的时间后就结束了,下一个线程任务到来还需要重新创建线程,这样线程数量特别庞大的时候,频繁的创建线程和销毁线程 ...
- Java并发包学习一 ThreadFactory介绍
ThreadFactory翻译过来是线程工厂,顾名思义,就是用来创建线程的,它用到了工厂模式的思想.它通常和线程池一起使用,主要用来控制创建新线程时的一些行为,比如设置线程的优先级,名字等等.它是一个 ...
- java并发编程——Excutor
概述 Excutor这个接口用的不多,但是ThreadPoolExcutor这个就用的比较多了,ThreadPoolExcutor是Excutor的一个实现.Excutor体系难点没有,大部分的关键点 ...
- JDK1.7中的ThreadPoolExecutor源代码剖析
JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...
- CentOS安装JDK-1.7
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. #准备工作# 准备用rpm下载前,看系统是否已经安装有JDK,如果没有则进入正式安装步骤. # rpm -qa | grep jd ...
- ubuntu 14.04 配置 jdk1.8
自己在Ubuntu中安装jdk1.8的步骤,记录以方便以后查询. 将下载好的jdk安装包移到/usr/local目录中(我喜欢将自己安装的软件放在/usr/local目录中),解压缩 sudo tar ...
- Linux下安装jdk1.7、Apache-tomcat7
首先说明下我的主机环境:主机:32位win7 虚拟机:VMware Workstation10.0.1 linux:红帽子centos6.4 jdk1.7 Apache-tomcat7 java环境需 ...
- Linux配置JDK1.7和Resin4.0
1.安装JDK1.7 (1)下载 官网下载路径:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-18802 ...
随机推荐
- docker之container
转自:https://www.cnblogs.com/jsonhc/p/7760144.html 运行一个container的本身就是开启一个具有独立namespace的进程 进程有自己的网络,文件系 ...
- java输出自身源代码
如何通过运行程序输出程序源码? 下面是JAVA实现 public class Quine { public static void main(String[] args) { char q = 34; ...
- Visual Studio for Mac 使用笔记
无法打开控制台运行程序 Project -> Project Options -> Run -> Configurations -> Default 勾选Run on exte ...
- RabbitMQ系列教程之二:工作队列(Work Queues)(转载)
RabbitMQ系列教程之二:工作队列(Work Queues) 今天开始RabbitMQ教程的第二讲,废话不多说,直接进入话题. (使用.NET 客户端 进行事例演示) ...
- Python中os模块使用方法
os模块提供了对系统环境.文件.目录等操作系统级的接口函数.本文主要描述os模块和os.path模块常用函数以及常用实例. os模块函数 os.getcwd() 获取当前工作的目录. os.listd ...
- LeetCode OJ 15. 3Sum
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 吴裕雄 python 机器学习-KNN算法(1)
import numpy as np import operator as op from os import listdir def classify0(inX, dataSet, labels, ...
- /WebRoot/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- keras初探
1.对网络的理解: 2.怎样训练,输入已经数据,经过训练,输入测试数据,得到相似数据 3.RNNs 循环神经网络
- RealtimeRendering III
[RealtimeRendering III] 1.砖块渲染实例. 1)brick & mortar diffuse texture. 2)brick & mortar gloss t ...