线程池类结构



1.Executor是顶级接口,有一个execute方法。

2.ExecutorService接口提供了管理线程的方法。

3.AbstractExecutorService管理普通线程,SchedulerExecutorService管理定时任务。

简单的示例

public class MyThread46 {
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
final List<Integer> l = new LinkedList<Integer>();
ThreadPoolExecutor tp = new ThreadPoolExecutor(100, 100, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(20000));
final Random random = new Random();
for (int i = 0; i < 20000; i++)
{
tp.execute(new Runnable()
{
public void run()
{
l.add(random.nextInt());
}
});
}
tp.shutdown();
try
{
tp.awaitTermination(1, TimeUnit.DAYS);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(System.currentTimeMillis() - startTime);
System.out.println(l.size());
}
}

运行结果如下

52
19919

ThreadPoolExecutor七个参数

public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)

1.corePoolSize

线程池当前可以存在的线程数量

2.maximumPoolSize

线程池允许的最大线程数量

3.keepAliveTime

当线程数量比corePoolSize大时才会起作用,终止前的空余线程等待的最长时间。

4.unit

keepAliveTime的时间单位

5.workQueue

存储未被执行的任务

6.threadFactory

executor创建新线程时使用的工厂

7.handler

当执行被阻塞时使用handler

corePoolSize与maximumPoolSize的关系

1.池中线程数小于corePoolSize,新任务都不排队而是直接添加新线程。

2.池中线程数大于等于corePoolSize,workQueue未满,将新任务加入workQueue而不是添加新线程。

3.池中线程数大于等于corePoolSize,workQueue已满,但是线程数小于maximumPoolSize,添加新的线程来处理被添加的任务。

4.池中线程数大于等于corePoolSize,workQueue已满,并且线程数大于等于maximumPoolSize,新任务被拒绝,使用handler处理被拒绝的任务。

Executors

1.newSingleThreadExecutos() 单线程线程池

    public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}

来新任务就排队,workQueue采用了无界队列LinkedBlockingQueue

示例代码如下

public class MyThread47{
static ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); public static void main(String[] args) {
for(int i =0;i<10;i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() { @Override
public void run() {
try {
System.out.println(index);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
} }

运行结果如下

0
1
2
3
4
5
6
7
8
9

2.newFixedThreadPool(int nThreads) 固定大小线程池

    public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}

固定大小线程池和单线程线程池类似,可以手动指定线程数量

示例代码如下

public class MyThread48 {
public static void main(String[] args) {
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {
final int index = i; fixedThreadPool.execute(new Runnable() { @Override
public void run() {
try {
System.out.println(index);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}

运行结果如下

0
1
2
3
4
5
6
8
7
9

3.newCachedThreadPool() 无界线程池

    public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}

有多少任务来直接执行,线程池最大数量Integer.MAX_VALUE,60s自动回收空闲线程。

示例代码如下

public class MyThread49 {
public static void main(String[] args) {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} cachedThreadPool.execute(new Runnable() { @Override
public void run() {
System.out.println(index);
}
});
}
}
}

运行结果如下

0
1
2
3
4
5
6
7
8
9

最新文章

  1. JavaScript Object对象
  2. 解决win10卡顿现象
  3. POJ 1019 Number Sequence
  4. Robot Framework自动化测试(三)---Selenium API
  5. CSS3 filter:drop-shadow滤镜与box-shadow区别应用 抄的
  6. ControlsFX8.0.2中对话框无法判断是否显示的修改
  7. Android开发之Fragment的介绍、使用及生命周期
  8. Unity3d shader之次表面散射(Subsurface Scattering)
  9. ios使用openUrl进行应用跳转
  10. RH253读书笔记(4)-Lab 4 The Domain Name System
  11. !DOCTYPE 文档类型声明
  12. (一二〇)CALayer的一些特性
  13. 看看redis中那些好玩的module (sql on redis, bf/cf on redis)
  14. 最好的前端API备忘单整理
  15. js面向对象、创建对象的工厂模式、构造函数模式、原型链模式
  16. slice()
  17. Android之sqlite3命令行简单使用
  18. UNIX环境编程学习笔记(19)——进程管理之fork 函数的深入学习
  19. ES6学习--对象属性的遍历
  20. 安装nvm之后node不可用,“node”不是内部或外部命令,也不是可运行的程序或批处理文件(ng)

热门文章

  1. nginx负载均衡策略url_hash配置方法
  2. 超实用,Linux中查看文本的小技巧
  3. [转载]关于ActiveMQ集群
  4. java中的异常 try catch
  5. 浅谈IDEA集成SSM框架(SpringMVC+Spring+MyBatis)
  6. Mybatis mapper动态代理的原理详解
  7. Zabbix-Web监控介绍篇
  8. d3.js制作蜂巢图表带动画效果
  9. 标准pcm数据(正弦波、方波、三角波)解读
  10. Java连载24-break语句、continue语句、输出质数练习