线程池-jdk1.8 Executors创建线程池的几种方法

目录

1、newFixedThreadPool - 定长线程池

2、newSingleThreadExecutor - 单一线程池

3、newCachedThreadPool - 缓存线程池

4、newScheduledThreadPool - 调度线程池

5、newSingleThreadScheduledExecutor - 单线程调度线程池

6、newWorkStealingPool - 抢占操作线程池


1、newFixedThreadPool - 定长线程池

创建一个线程池,该线程池重用在共享无界队列上运行的固定数量的线程。在任何时候,线程最多都是活动的处理任务。如果在所有线程都处于活动状态时提交其他任务,它们将在队列中等待,直到有线程可用。如果任何线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将有一个新线程替代它。池中的线程将一直存在,直到显式关闭。

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

2、newSingleThreadExecutor - 单一线程池

创建一个执行器,该执行器使用一个工作线程在无界队列上运行。(但是请注意,如果此单线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将使用一个新线程代替它。)任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。与其他等效的newFixedThreadPool(1)不同,返回的执行器保证不可重新配置以使用其他线程。

    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));
    }

 newSingleThreadExecutor和newFixedThreadPool(1)区别

Executors的newSingleThreadExecutor和newFixedThreadPool(1)区别_Ahuuua的博客-CSDN博客

3、newCachedThreadPool - 缓存线程池

创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程。这些池通常会提高执行许多短期异步任务的程序的性能。调用execute将重用以前构造的线程(如果可用)。如果没有可用的现有线程,将创建一个新线程并将其添加到池中。60秒未使用的线程将被终止并从缓存中删除。因此,闲置足够长时间的池不会消耗任何资源。请注意,可以使用ThreadPoolExecutor构造函数创建具有类似属性但不同细节(例如超时参数)的池。

    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);
    }

4、newScheduledThreadPool - 调度线程池

创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。

参数:

corePoolSize – 池中要保留的线程数,即使它们处于空闲状态

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

5、newSingleThreadScheduledExecutor - 单线程调度线程池

创建一个单线程执行器,该执行器可以安排命令在给定延迟后运行,或定期执行。(但是请注意,如果此单线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将使用一个新线程代替它。)任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。与其他等效的newScheduledThreadPool(1)不同,返回的执行器保证不可重新配置以使用其他线程。

    public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }
    public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));
    }

6、newWorkStealingPool - 抢占操作线程池

创建一个线程池,该线程池维护足够多的线程以支持给定的并行度级别,并且可以使用多个队列来减少争用。并行级别对应于积极参与或可参与任务处理的最大线程数。线程的实际数量可能会动态增长和收缩。工作窃取池不保证提交任务的执行顺序。

参数:

并行度——目标并行度级别

    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);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ahuuua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值