java线程池的创建

newCachedThreadPool

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
}
  • 核心线程数corePoolSize=0;
  • 最大线程数maxinumPoolSize=Integer.MAX_VALUE;
  • 空闲线程存活时间keepAliveTime=60;
  • 存放线程任务队列workQueue=new SynchronousQueue()

由于任务队列采用SynchronousQueue:

一个不缓存任务的阻塞队列,生产者放入一个任务必须等到消费者取出这个任务。也就是说新任务进来时,不会缓存,而是直接被调度执行该任务,如果没有可用线程,则创建新线程,如果线程数量达到maxPoolSize,则执行拒绝策略。

也就是说,只要有任务到达,立即从线程池中获取空闲线程,如果没有空闲,则立即创建新的线程。

由于,核心线程数corePoolSize=0;

由于maxinumPoolSize=Integer.MAX_VALUE,意味着他的线程数几乎可以无限增加。

而keepAliveTime是指非核心线程的空闲时间超出该参数,该线程就会被终止。

因此,该线程池所有线程都是非核心线程,因此keepAliveTime=60对所有线程都有效。

因此,在有执行时间很短的大量任务需要执行时,newCachedThreadPool能够很好地复用运行中的线程(空闲并且还存活)资源来提高系统的运行效率。

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
  • 核心线程数corePoolSize=nThreads;
  • 最大线程数maxinumPoolSize=nThreads;
  • 空闲线程存活时间keepAliveTime=0;
  • 存放线程任务队列workQueue=new LinkedBlockingQueue()

corePoolSize=maxinumPoolSize=nThreads(固定长度),不存在非核心线程的扩充,当没有空闲线程时,任务在队列中等待,当队列满时,触发拒绝机制。(如果maxinumPoolSize>corePoolSize,当队列满时,会新建线程:非核心线程)

LinkedBlockingQueue是基于链表的无界阻塞队列(其实最大容量为Interger.MAX),按照FIFO排序。由于该队列的近似无界性,当线程池中线程数量达到corePoolSize后,再有新任务进来,会一直存入该队列,而不会去创建新线程直到maxPoolSize,因此使用该工作队列时,参数maxPoolSize其实是不起作用的。

非核心线程:个数=maxinumPoolSize-corePoolSize,只有当任务队列满时,才会触发创建非核心线程。

而之前讲过:keepAliveTime参数只针对非核心线程有效,而核心线程属于常驻线程,没有存活时间限制。

因为该线程池corePoolSize=maxinumPoolSize,非核心线程数为0,因此空闲线程存活时间keepAliveTime以及其单位都是没有意义的。

newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

public static ScheduledExecutorService newScheduledThreadPool(
        int corePoolSize, ThreadFactory threadFactory) {
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue());
}

public ScheduledThreadPoolExecutor(int corePoolSize,
                                       ThreadFactory threadFactory) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue(), threadFactory);
    }

newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
}
  • 核心线程数corePoolSize=1;
  • 最大线程数maxinumPoolSize=1;
  • 空闲线程存活时间keepAliveTime=0;
  • 存放线程任务队列workQueue=new LinkedBlockingQueue()

该线程池和newFixedThreadPool线程池类似,只是将核心线程数设为1.

由于LinkedBlockingQueue容量近似无限大的特征,当有新的任务到来时,首先判断唯一的线程是否空闲,如果空闲,则处理任务,否则加入队列等待。

该线程池可以保证所有任务按指定顺序(FIFO, LIFO, 优先级)执行。

注:当唯一的线程停止或发生异常时,该线程池会重新创建一个新的进程。

newWorkStealingPool (1.8引入)

public static ExecutorService newWorkStealingPool(int parallelism) {
    return new ForkJoinPool
        (parallelism,
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
}
public static ExecutorService newWorkStealingPool() {
    return new ForkJoinPool
        (Runtime.getRuntime().availableProcessors(),
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
}
public ForkJoinPool(int parallelism,
                    ForkJoinWorkerThreadFactory factory,
                    UncaughtExceptionHandler handler,
                    boolean asyncMode) {
    this(checkParallelism(parallelism),
         checkFactory(factory),
         handler,
         asyncMode ? FIFO_QUEUE : LIFO_QUEUE,
         "ForkJoinPool-" + nextPoolId() + "-worker-");
    checkPermission();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值