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 ...
随机推荐
- Linux命令之lsof
1.lsof简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. ...
- Packets 1037A(二进制数)
分析:看这个数有多少位二进制数 #include<cstdio> int main() { int n; while(~scanf("%d",&n)) { ; ...
- 4.Python文件操作
文件内需要写入的内容 Seems the love I’ve ever known 看来,过去我所知道的爱情 Has always been the most destructive kind 似乎总 ...
- Python使用xlwt模块 操作Excel文件
导出Excel文件 1. 使用xlwt模块 import xlwt import xlwt # 导入xlwt # 新建一个excel文件 file = xlwt.Workbook() # ...
- 二叉堆复习(包括d堆)
要期中考了……我真的是什么也不会啊,书都没看过TAT. 好吧整理一下二叉堆,这里就以最大堆为例好了. 首先二叉堆其实是一棵CBT,满足父节点的键值大于左右子节点的键值(wikipedia把这个叫键值, ...
- 原生js上传文件,使用new FormData()
当创建一个内容较多的表单,表单里面又有了文件上传,文件上传也需要表单提交,单一的上传文件很好操作: <form action="接口" enctype="multi ...
- pycharm 的包路径设置export PYTHONPATH=$PYTHONPATH
我们使用pycharm的时候,经常会因为要链接(import)其他自己写的包,因此在pycharm的时候经常会 报错,就是找不到自己的包,在命令行下常用,export PYTHONPATH=$PYTH ...
- yii gii配置ip限制使用gii
<?php $config = [ 'components' => [ 'request' => [ // !!! insert a secret key in the follow ...
- opencv矩阵操作
1.初始化矩阵: 方式一.逐点赋值式: CvMat* mat = cvCreateMat( 2, 2, CV_64FC1 ); cvZero( mat ); cvmSet( mat, 0, 0, 1 ...
- MATLAB二维相机标定的解决方案 calibration
第一步,在命令行下面输入cameraCalibrator,启动MATLAB相机标定.相机矫正界面 cameraCalibrator 第二步:拍照.如果你是做相机标定,你应该知道,你需要一些calibr ...