博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程——newCachedThreadPool线程池
阅读量:4671 次
发布时间:2019-06-09

本文共 2818 字,大约阅读时间需要 9 分钟。

newCachedThreadPool线程池:

理解:
  1).newCachedThreadPool可以创建一个无限大小的线程池(实际上是一个可缓存线程池)。
      可以通过Executors的静态方法创建线程池:
        
public static ExecutorService newCachedThreadPool() 或者
        
 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) 。
  2).返回的是
ExecutorService对象线程池。
  3).newCachedThreadPool在执行过程中通常会创建与所需数量相同的线程,然后在它回收旧线程时停止创建新线程,因此它是合理的Executor的首选。
     只有当这种方式会引发问题时(内存溢出),你才需要切换到newFixedThreadPool()。
  4).无限制不代表不可以复用线程,假设说一开始进来10个任务,启动了10个线程,10个任务结束后,然后又来了5个任务(线程还没被回收),
      这时候会复用之前的线程,不会新起线程,这就达到了使用“线程池”的意义。
使用场景:
  1. 耗时较短的任务。
  2. 任务处理速度 > 任务提交速度 ,这样才能保证不会不断创建新的进程,避免内存被占满。
//首先看Executors中通过newCachedThreadPool创建线程池的方法的源码//此方法是通过new一个ThreadPoolExecutor来创建的//这个构造方法有5个参数://第一个参数corePoolSize(核心池大小)为0,//第二个参数maximumPoolSize(最大线程池大小)为Integer.MAX_VALUE无限大,源码中是:public static final int   MAX_VALUE = 0x7fffffff;//第三个参数keepAliveTime(线程存货的时间)是60秒,意味着线程空闲时间超过60秒就会被杀死。//第五个参数采用SynchronousQueue装等待的任务,这个阻塞队列没有存储空间,这意味着只要有请求到来,就必须要找到一条工作线程处理他,//    如果当前没有空闲的线程,那么就会再创建一条新的线程。public class Executors {       public static ExecutorService newCachedThreadPool() {        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,                                      60L, TimeUnit.SECONDS,                                      new SynchronousQueue
()); }}//ThreadPoolExecutor类中此构造函数源码:public class ThreadPoolExecutor extends AbstractExecutorService { public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue
workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }}

 

下面是Thinking in java书中的例子:
public class LiftOff implements Runnable {    protected int countDown = 5;    private static int taskCount = 0;    private final int id = taskCount++;    public LiftOff() {    }    public LiftOff(int countDown) {        this.countDown = countDown;    }    public String status() {        return "# " + id + " ( " + (countDown > 0 ? countDown : "LiftOff !") + " )";    }    @Override    public void run() {        while (countDown-- > 0) {            System.out.println(status());            Thread.yield();        }    }}
public class CachedThreadPool {    public static void main(String[] args) {        ExecutorService executorService = Executors.newCachedThreadPool();        for (int i = 0; i < 3; i++) {            executorService.execute(new LiftOff());        }    }}结果:# 2 ( 4 )# 0 ( 4 )# 1 ( 4 )# 2 ( 3 )# 0 ( 3 )# 2 ( 2 )# 1 ( 3 )# 2 ( 1 )# 0 ( 2 )# 2 ( LiftOff ! )# 1 ( 2 )# 0 ( 1 )# 1 ( 1 )# 0 ( LiftOff ! )# 1 ( LiftOff ! )

 

 

转载于:https://www.cnblogs.com/whx20100101/p/9862382.html

你可能感兴趣的文章
玩一下易语言 "和"字有多种读音,注定了它的重要性!!
查看>>
Python中的单例模式的几种实现方式的及优化
查看>>
【转】hadoop机架感知
查看>>
Oracle、DB2、SQLSERVER、Mysql、Access分页SQL语句梳理
查看>>
UVa 10806 Dijkstra,Dijkstra(最小费用最大流)
查看>>
Java排序算法之直接选择排序
查看>>
《鸟哥的Linux私房菜 基础学习篇(第四版)》 第23章 XWindows设定介绍 笔记
查看>>
js同比例缩放图片
查看>>
本机不装Oracle,使用plsql连接远程Oracle的方法
查看>>
bzoj1562[NOI2009] 变换序列
查看>>
LODOP在页面不同位置输出页眉页脚
查看>>
Android Thread 官方说明
查看>>
瞎折腾-CentOS 7.4 编译4.16.2版kernel 并安装
查看>>
[转]如何才能在 IIS 7.5 使用 Windows PowerShell Snap-In 功能
查看>>
Selenium 调用IEDriverServer打开IE浏览器
查看>>
Nginx 访问日志配置
查看>>
python中while与else的联姻
查看>>
AtCoder 杂题训练
查看>>
javascript 200列(3)
查看>>
随手练——打印折痕方向
查看>>