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 ...
随机推荐
- React Native多语言
https://medium.com/@danielsternlicht/adding-localization-i18n-g11n-to-a-react-native-project-with-rt ...
- Delphi 三层TDataSetProvider
在Delphi想使用三层架构或者使用TClientDataSet控件,一般都需要引用TDataSetProvider控件,现对TDataSetProvider控件的Options属性值做一个简单的分析 ...
- css-图片垂直居中
1. img { vertical-align: middle; } 2. <body> <div> <img src="1.jpg" alt=& ...
- How to Pronounce AR, ORN, etc.
How to Pronounce AR, ORN, etc. Share Tweet Share The R consonant can be really tricky. In this vide ...
- pip安装提示PermissionError: [WinError 5]错误问题解决
操作环境 Python3.6 + Winodws7 问题现象 新安装python3.6版本后使用pip安装第三方模块失败,报错信息如下: C:\Users\linyfeng>pip inst ...
- [PC]PHPCMS二次开发指南(上)
------------------------------------------------------------------------------------- PHPCMS本身功能已经很完 ...
- 使用arguments对象验证函数的参数是否合法
<script>function sum(arg1,arg2) //加法函数{ var realArgCount = arguments.length; //调用函数时传递的实参个数 va ...
- Java 中 == 和 equals 的区别
有一段时间,== 和 equals 的区别一直困扰着我.因为涉及到Java的内存机制,然而Java的内存机制又是比较抽象的东西,所以对那时候的我来说,实在是很难理解. == 和 equals 最大的区 ...
- python基础学习Day10 函数形参的动态参数、*args **kwargs 命名空间 global 与 nonlocal
一.函数形参的动态参数 原因:为了拓展,对于实参数量不固定,故需要万能参数,即动态参数, *args **kwargs # def sum1(*args): # 在函数定义时,在*位置参数,聚合. ...
- windows 允许空密码登陆
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 这个注册表键值下的limitblankpassworduse项 修改为0或者1