Executor、ExecutorService、ThreadPoolExecutor
1、Executor
Executor接口中中只有一个方法
执行已提交的Runnable任务对象。
ExecutorService pool1 = Executors.newFixedThreadPool(5);
ExecutorService pool2 = Executors.newCachedThreadPool();
ExecutorService pool3 = Executors.newSingleThreadExecutor();
ExecutorService pool = Executors.newScheduledThreadPool(1);
这几种直接进入最终方法看吧
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,//coreSize
int maximumPoolSize,//maxSize
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,//有界队列和无界队列
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {//拒绝策略或者其他操作,一个对象,可以用new RejectedExecutionHandler(),或者自定义
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
Executor、ExecutorService、ThreadPoolExecutor的更多相关文章
- 多线程——Executor、ExecutorService、Executors三者的区别
Executor.ExecutorService.Executors三者的区别: 层次关系: public interface ExecutorService extends Executor {} ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)
1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes th ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes the given c ...
- 001-多线程-JUC线程池-线程池架构-Executor、ExecutorService、ThreadPoolExecutor、Executors
一.概述 1.1.线程池架构图 1. Executor 它是"执行者"接口,它是来执行任务的.准确的说,Executor提供了execute()接口来执行已提交的 Runnable ...
- Java多线程之Executor、ExecutorService、Executors、Callable、Future与FutureTask
1. 引子 初学Java多线程,常使用Thread与Runnable创建.启动线程.如下例: Thread t1 = new Thread(new Runnable() { @Override pub ...
- (转)java中Executor、ExecutorService、ThreadPoolExecutor介绍
转自: http://blog.csdn.net/linghu_java/article/details/17123057 ScheduledThreadPoolExecutor介绍: http:// ...
- 003-多线程-JUC线程池-几种特殊的ThreadPoolExecutor【newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor、newScheduledThreadPool】
一.概述 在java doc中,并不提倡我们直接使用ThreadPoolExecutor,而是使用Executors类中提供的几个静态方法来创建线程池: 以下方法是Executors下的静态方法,Ex ...
- Java - 多线程Callable、Executors、Future
http://blog.csdn.net/pipisorry/article/details/44341579 Introduction Callable接口代表一段能够调用并返回结果的代码; Fut ...
- Java并发编程 - Runnbale、Future、Callable 你不知道的那点事(二)
Java并发编程 - Runnbale.Future.Callable 你不知道的那点事(一)大致说明了一下 Runnable.Future.Callable 接口之间的关系,也说明了一些内部常用的方 ...
随机推荐
- 835. Image Overlap
Two images A and B are given, represented as binary, square matrices of the same size. (A binary ma ...
- APP间传递消息
https://www.jianshu.com/p/d640ccfcea5f 1: URL Scheme 常用的App间传值方式.常见于分享等. 2: Keychain 借助系统类 Keychain ...
- 关于ManyToMany的一点补充
1.先看model的定义 user表: class User(models.Model): """ 用户表 """ username = m ...
- java 中什么是aop
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)
本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...
- Java基础之断言
断言是在Java 1.4中引入的.它能让你验证假设.如果断言失败(即返回false),就会抛出AssertionError(如果启用断言). 什么时候使用断言? 断言不应该用于验证输入数据到一个pub ...
- leetcode-914-卡牌分组
题目描述: 给定一副牌,每张牌上都写着一个整数. 此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组: 每组都有 X 张牌. 组内所有的牌上都写着相同的整数. 仅当你可选的 ...
- 【xsy1197】 树 二分+点分树+二分
题目大意:给你一棵$n$个点的带权树和正整数$K$,求每个点到其它所有点距离中第$K$大的数值. 其中,边权$≤10000$,$n≤50000$. 我们通过原树构建一棵点分治树,令$fa[u]$为$u ...
- python 批量模块导入(笔记)
环境:python3.6 目的:根据列表['os', 'sys', 're']中的字符串导入对应模块 from importlib import import_module modules = ['o ...
- (转)测试rootvg卷组的镜像的官方做法
测试rootvg卷组的镜像的官方做法 这篇文档介绍了测试rootvg卷组镜像的方法,此文档仅使用于带有热插拔硬盘的pSeries服务器.由于rootvg卷组包含有AIX操作系统,在做卷组镜像配置上比非 ...