开发者

线程池之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 LinkedBlockingQpythonueue<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)区别

3、newCachedThreadPool - 缓存线程池

创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程。

这些池通常会提高执行许多短期异步任务的程序的性能。

调用execute将重用以前构造的线程(如果可用)。

如果没有http://www.devze.com可用的现有线程,将创建一个新线程并将其添加到池中。

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 DelegatedScheduledExecutandroidorService
            (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 FmEBWDBPmorkJoinPool
            (parallelism,
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }
    public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerTpythonhreadFactory,
             null, true);
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜