Executors类中创建线程池的几种方法的分析

来源:互联网 发布:莫高冰酒怎么样 知乎 编辑:程序博客网 时间:2024/05/18 09:59

Executors类中创建线程池的几种方法的分析

要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,很有可能配置的线程池不是较优的,因此在Executors类里面提供了一些静态工厂,生成一些常用的线程池。

1、newFixedThreadPool:创建固定大小的线程池。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。

    /*    函数功能:创建一个固定长度的的线程池,用于保存任务的阻塞队列为无限制长度的LinkedBlockingQueue。              线程池中的线程将会一直存在除非线程池shutdown,即线程池中的线程没有受到存活时间的限制。    */    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);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2、newCachedThreadPool:创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。

在Executors类中此方法的代码如下:

    /*        函数功能:创建一个线程池,这个线程池的corePoolSize的大小为零,maxPoolSize为Integer.MAX_VALUE.            即对于线程池,只要需要就可以创建新的线程。不过如果有空闲线程存在则就会重用此线程。            此线程池的线程都有一定的存活时间。即如果线程已经有60s没有被使用则就会被移除线程池。    */    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);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3、newSingleThreadExecutor:创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务。如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。

    //创建一个单工作线程且无边界的队列的线程池。如果执行期间此线程挂掉了,则如果需要就会产生一个新线程来顺序执行任务。    //任何时候都不会有超过一个线程是存活的    public static ExecutorService newSingleThreadExecutor() {        return new FinalizableDelegatedExecutorService            (new ThreadPoolExecutor(1, 1,                                    0L, TimeUnit.MILLISECONDS,                                    new LinkedBlockingQueue<Runnable>()));    }    /*        函数功能:与newSingleThreadExecutor()功能一样,只是指定了线程工厂来生产新的线程。    */    public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {        return new FinalizableDelegatedExecutorService            (new ThreadPoolExecutor(1, 1,                                    0L, TimeUnit.MILLISECONDS,                                    new LinkedBlockingQueue<Runnable>(),                                    threadFactory));    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4、newScheduledThreadPool:创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。

    //创建一个线程池,该线程池提供延时执行任务或者是周期性执行任务的功能。    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {        return new ScheduledThreadPoolExecutor(corePoolSize);    }    public static ScheduledExecutorService newScheduledThreadPool(            int corePoolSize, ThreadFactory threadFactory) {        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

ScheduledThreadPoolExecutor 类中的构造函数如下:

    /*        函数功能:根据指定的corePoolSize的大小来创建线程池,maxPoolSize的大小为Integer.MAX_VALUE,线程没有存活时间的限制    */    public ScheduledThreadPoolExecutor(int corePoolSize) {        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,              new DelayedWorkQueue());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5、newSingleThreadScheduledExecutor:创建一个单线程的线程池。此线程池支持定时以及周期性执行任务的需求。

    //创建一个单线程的线程池,提供任务延时执行或周期性执行的功能。    //如果该线程在线程池运行期间挂掉,则如果需要则会产生一个新的线程来顺序执行任务。    public static ScheduledExecutorService newSingleThreadScheduledExecutor() {        return new DelegatedScheduledExecutorService            (new ScheduledThreadPoolExecutor(1));    }    public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {        return new DelegatedScheduledExecutorService            (new ScheduledThreadPoolExecutor(1, threadFactory));    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

ScheduledThreadPoolExecutor 类中的构造函数如下:

    /*        函数功能:根据指定的corePoolSize的大小来创建线程池,maxPoolSize的大小为Integer.MAX_VALUE.    */    public ScheduledThreadPoolExecutor(int corePoolSize) {        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,              new DelayedWorkQueue());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

小结

以上几种创建线程池的方法我们都需要有一个大体的影响哈。

原创粉丝点击