跟我学Java多线程——ThreadPoolExecutor(线程池)

什么是线程池

 

    多线程开发中,由于线程数量多,并且每个线程执行一段时间就结束,所以要频繁的创建线程,但是这样频繁的创建线程会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间。在这种情况下,人们就想要一种可以线程执行完后不用销毁,同时该线程还可以去执行其他任务,在这样的情况下线程池就出现了。

 

    线程池就是线程的池子,任务提交到线程池后,就从线程池中取出一个空闲的线程为之服务,服务完后不销毁该线程,而是将该线程还回到线程池中。

 

    在线程池的编程模式下,任务是提交给整个线程池,而不是直接交给某个线程,线程池在拿到任务后,它就在内部找有无空闲的线程,再把任务交给内部某个空闲的线程,如果没有空闲的进程,任务就处于等待状态,这就是封装。

 

    使用了线程池后减少了创建和销毁线程的次数,每个线程都可以被重复利用,可执行多个任务;同时可以根据系统的承受能力,调整线程池中线程的数目,避免出现将系统内存消耗完毕这样的情况出现。

 

 

ThreadPoolExecuto例子

 

    我们先通过一个简单的线程池的例子先来对ThreadPoolExecuto进行一个整体的认识,我们构造了一个正常线程数量为5,最大线程池数量为10,任务缓存队列5的线程池,让这个线程池来执行15个任务的demo

 

    先来看下MyRunnable类,用来记录当前正在执行的任务数:

[java]  view plain  copy
  1. package com.tgb.threadpool;  
  2.   
  3. /** 
  4.  * 线程执行 
  5.  * @author kang 
  6.  * 
  7.  */  
  8. public class MyRunnable implements Runnable {  
  9.   
  10.     // 正在执行的任务数  
  11.     private int num;  
  12.   
  13.     public MyRunnable(int num) {  
  14.         this.num = num;  
  15.     }  
  16.   
  17.     @Override  
  18.     public void run() {  
  19.         System.out.println("正在执行的MyRunnable " + num);  
  20.         try {  
  21.             Thread.currentThread().sleep(4000);  
  22.         } catch (InterruptedException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         System.out.println("MyRunnable " + num + "执行完毕");  
  26.   
  27.     }  
  28.   
  29. }  
 

    线程池客户端:

[java]  view plain  copy
  1. package com.tgb.threadpool;  
  2.   
  3. import java.util.concurrent.ArrayBlockingQueue;  
  4. import java.util.concurrent.ThreadPoolExecutor;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. /** 
  8.  * 线程池调用客户端 
  9.  * @author kang 
  10.  * 
  11.  */  
  12. public class ThreadPoolExecutorTest {  
  13.   
  14.     //池中所保存的线程数,包括空闲线程。  
  15.     final static int corePoolSize = 5;  
  16.     //池中允许的最大线程数。  
  17.     final static int maximumPoolSize = 10;  
  18.     //当线程数大于核心线程时,此为终止前多余的空闲线程等待新任务的最长时间  
  19.     final static long keepAliveTime = 200;  
  20.     //执行前用于保持任务的队列5,即任务缓存队列  
  21.     final static ArrayBlockingQueue<Runnable> workQueue =new ArrayBlockingQueue<Runnable>(5);  
  22.       
  23.     public static void main(String[] args) {  
  24.         //构建一个线程池,正常线程数量为5,最大线程数据为10,等待时间200  
  25.         ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(  
  26.                 corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MINUTES, workQueue);  
  27.           
  28.         //线程池去执行15个任务  
  29.         for (int i = 0; i < 15; i++) {  
  30.             MyRunnable myRunnable = new MyRunnable(i);  
  31.             threadPoolExecutor.execute(myRunnable);  
  32.             System.out.println("线程池中现在的线程数目是:"+threadPoolExecutor.getPoolSize()+",  队列中正在等待执行的任务数量为:"+  
  33.                     threadPoolExecutor.getQueue().size());  
  34.         }  
  35.         //关掉线程池  
  36.         threadPoolExecutor.shutdown();  
  37.           
  38.     }  
  39.       
  40. }  

    执行结果:

[cpp]  view plain  copy
  1. 正在执行的MyRunnable 0  
  2. 线程池中现在的线程数目是:1,  队列中正在等待执行的任务数量为:0  
  3. 线程池中现在的线程数目是:2,  队列中正在等待执行的任务数量为:0  
  4. 正在执行的MyRunnable 1  
  5. 线程池中现在的线程数目是:3,  队列中正在等待执行的任务数量为:0  
  6. 正在执行的MyRunnable 2  
  7. 线程池中现在的线程数目是:4,  队列中正在等待执行的任务数量为:0  
  8. 正在执行的MyRunnable 3  
  9. 线程池中现在的线程数目是:5,  队列中正在等待执行的任务数量为:0  
  10. 正在执行的MyRunnable 4  
  11. 线程池中现在的线程数目是:5,  队列中正在等待执行的任务数量为:1  
  12. 线程池中现在的线程数目是:5,  队列中正在等待执行的任务数量为:2  
  13. 线程池中现在的线程数目是:5,  队列中正在等待执行的任务数量为:3  
  14. 线程池中现在的线程数目是:5,  队列中正在等待执行的任务数量为:4  
  15. 线程池中现在的线程数目是:5,  队列中正在等待执行的任务数量为:5  
  16. 线程池中现在的线程数目是:6,  队列中正在等待执行的任务数量为:5  
  17. 正在执行的MyRunnable 10  
  18. 线程池中现在的线程数目是:7,  队列中正在等待执行的任务数量为:5  
  19. 正在执行的MyRunnable 11  
  20. 线程池中现在的线程数目是:8,  队列中正在等待执行的任务数量为:5  
  21. 正在执行的MyRunnable 12  
  22. 线程池中现在的线程数目是:9,  队列中正在等待执行的任务数量为:5  
  23. 正在执行的MyRunnable 13  
  24. 线程池中现在的线程数目是:10,  队列中正在等待执行的任务数量为:5  
  25. 正在执行的MyRunnable 14  
  26. MyRunnable 0执行完毕  
  27. 正在执行的MyRunnable 5  
  28. MyRunnable 3执行完毕  
  29. MyRunnable 4执行完毕  
  30. MyRunnable 2执行完毕  
  31. 正在执行的MyRunnable 7  
  32. 正在执行的MyRunnable 6  
  33. 正在执行的MyRunnable 8  
  34. MyRunnable 1执行完毕  
  35. 正在执行的MyRunnable 9  
  36. MyRunnable 12执行完毕  
  37. MyRunnable 11执行完毕  
  38. MyRunnable 10执行完毕  
  39. MyRunnable 14执行完毕  
  40. MyRunnable 13执行完毕  
  41. MyRunnable 5执行完毕  
  42. MyRunnable 9执行完毕  
  43. MyRunnable 8执行完毕  
  44. MyRunnable 6执行完毕  
  45. MyRunnable 7执行完毕  
    从执行结果来看,当线程池中线程的数量大于 5 时,便将任务放入任务缓存队列里面,任务缓存队列满了以后,变创建新的线程,来执行,如果让任务数量大于 15 ,程序就会报错,抛出任务拒绝异常,这时可以增大任务缓存队列,有兴趣的同学可以去试试。

 

    通过这个demo应该可以对线程池有了一个宏观的理解了吧,下面我们就继续深入的来学习ThreadPoolExecutor对象。

 

ThreadPoolExecutor源码

 

    java.uitl.concurrent.ThreadPoolExecutor这个类是线程池中最核心的一个类,所以我们就通过来ThreadPoolExecutor的源码来进一步理解线程池:

[java]  view plain  copy
  1. public class ThreadPoolExecutor extends AbstractExecutorService {  
  2.   
  3.     // Public constructors and methods  
  4.   
  5.     /** 
  6.      * Creates a new {@code ThreadPoolExecutor} with the given initial 
  7.      * parameters and default thread factory and rejected execution handler. 
  8.      * It may be more convenient to use one of the {@link Executors} factory 
  9.      * methods instead of this general purpose constructor. 
  10.      * 
  11.      * @param corePoolSize the number of threads to keep in the pool, even 
  12.      *        if they are idle, unless {@code allowCoreThreadTimeOut} is set 
  13.      * @param maximumPoolSize the maximum number of threads to allow in the 
  14.      *        pool 
  15.      * @param keepAliveTime when the number of threads is greater than 
  16.      *        the core, this is the maximum time that excess idle threads 
  17.      *        will wait for new tasks before terminating. 
  18.      * @param unit the time unit for the {@code keepAliveTime} argument 
  19.      * @param workQueue the queue to use for holding tasks before they are 
  20.      *        executed.  This queue will hold only the {@code Runnable} 
  21.      *        tasks submitted by the {@code execute} method. 
  22.      * @throws IllegalArgumentException if one of the following holds:<br> 
  23.      *         {@code corePoolSize < 0}<br> 
  24.      *         {@code keepAliveTime < 0}<br> 
  25.      *         {@code maximumPoolSize <= 0}<br> 
  26.      *         {@code maximumPoolSize < corePoolSize} 
  27.      * @throws NullPointerException if {@code workQueue} is null 
  28.      */  
  29.     public ThreadPoolExecutor(int corePoolSize,  
  30.                               int maximumPoolSize,  
  31.                               long keepAliveTime,  
  32.                               TimeUnit unit,  
  33.                               BlockingQueue<Runnable> workQueue) {  
  34.         this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,  
  35.              Executors.defaultThreadFactory(), defaultHandler);  
  36.     }  
  37.   
  38.     /** 
  39.      * Creates a new {@code ThreadPoolExecutor} with the given initial 
  40.      * parameters and default rejected execution handler. 
  41.      * 
  42.      * @param corePoolSize the number of threads to keep in the pool, even 
  43.      *        if they are idle, unless {@code allowCoreThreadTimeOut} is set 
  44.      * @param maximumPoolSize the maximum number of threads to allow in the 
  45.      *        pool 
  46.      * @param keepAliveTime when the number of threads is greater than 
  47.      *        the core, this is the maximum time that excess idle threads 
  48.      *        will wait for new tasks before terminating. 
  49.      * @param unit the time unit for the {@code keepAliveTime} argument 
  50.      * @param workQueue the queue to use for holding tasks before they are 
  51.      *        executed.  This queue will hold only the {@code Runnable} 
  52.      *        tasks submitted by the {@code execute} method. 
  53.      * @param threadFactory the factory to use when the executor 
  54.      *        creates a new thread 
  55.      * @throws IllegalArgumentException if one of the following holds:<br> 
  56.      *         {@code corePoolSize < 0}<br> 
  57.      *         {@code keepAliveTime < 0}<br> 
  58.      *         {@code maximumPoolSize <= 0}<br> 
  59.      *         {@code maximumPoolSize < corePoolSize} 
  60.      * @throws NullPointerException if {@code workQueue} 
  61.      *         or {@code threadFactory} is null 
  62.      */  
  63.     public ThreadPoolExecutor(int corePoolSize,  
  64.                               int maximumPoolSize,  
  65.                               long keepAliveTime,  
  66.                               TimeUnit unit,  
  67.                               BlockingQueue<Runnable> workQueue,  
  68.                               ThreadFactory threadFactory) {  
  69.         this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,  
  70.              threadFactory, defaultHandler);  
  71.     }  
  72.   
  73.     /** 
  74.      * Creates a new {@code ThreadPoolExecutor} with the given initial 
  75.      * parameters and default thread factory. 
  76.      * 
  77.      * @param corePoolSize the number of threads to keep in the pool, even 
  78.      *        if they are idle, unless {@code allowCoreThreadTimeOut} is set 
  79.      * @param maximumPoolSize the maximum number of threads to allow in the 
  80.      *        pool 
  81.      * @param keepAliveTime when the number of threads is greater than 
  82.      *        the core, this is the maximum time that excess idle threads 
  83.      *        will wait for new tasks before terminating. 
  84.      * @param unit the time unit for the {@code keepAliveTime} argument 
  85.      * @param workQueue the queue to use for holding tasks before they are 
  86.      *        executed.  This queue will hold only the {@code Runnable} 
  87.      *        tasks submitted by the {@code execute} method. 
  88.      * @param handler the handler to use when execution is blocked 
  89.      *        because the thread bounds and queue capacities are reached 
  90.      * @throws IllegalArgumentException if one of the following holds:<br> 
  91.      *         {@code corePoolSize < 0}<br> 
  92.      *         {@code keepAliveTime < 0}<br> 
  93.      *         {@code maximumPoolSize <= 0}<br> 
  94.      *         {@code maximumPoolSize < corePoolSize} 
  95.      * @throws NullPointerException if {@code workQueue} 
  96.      *         or {@code handler} is null 
  97.      */  
  98.     public ThreadPoolExecutor(int corePoolSize,  
  99.                               int maximumPoolSize,  
  100.                               long keepAliveTime,  
  101.                               TimeUnit unit,  
  102.                               BlockingQueue<Runnable> workQueue,  
  103.                               RejectedExecutionHandler handler) {  
  104.         this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,  
  105.              Executors.defaultThreadFactory(), handler);  
  106.     }  
  107.   
  108.     /** 
  109.      * Creates a new {@code ThreadPoolExecutor} with the given initial 
  110.      * parameters. 
  111.      * 
  112.      * @param corePoolSize the number of threads to keep in the pool, even 
  113.      *        if they are idle, unless {@code allowCoreThreadTimeOut} is set 
  114.      * @param maximumPoolSize the maximum number of threads to allow in the 
  115.      *        pool 
  116.      * @param keepAliveTime when the number of threads is greater than 
  117.      *        the core, this is the maximum time that excess idle threads 
  118.      *        will wait for new tasks before terminating. 
  119.      * @param unit the time unit for the {@code keepAliveTime} argument 
  120.      * @param workQueue the queue to use for holding tasks before they are 
  121.      *        executed.  This queue will hold only the {@code Runnable} 
  122.      *        tasks submitted by the {@code execute} method. 
  123.      * @param threadFactory the factory to use when the executor 
  124.      *        creates a new thread 
  125.      * @param handler the handler to use when execution is blocked 
  126.      *        because the thread bounds and queue capacities are reached 
  127.      * @throws IllegalArgumentException if one of the following holds:<br> 
  128.      *         {@code corePoolSize < 0}<br> 
  129.      *         {@code keepAliveTime < 0}<br> 
  130.      *         {@code maximumPoolSize <= 0}<br> 
  131.      *         {@code maximumPoolSize < corePoolSize} 
  132.      * @throws NullPointerException if {@code workQueue} 
  133.      *         or {@code threadFactory} or {@code handler} is null 
  134.      */  
  135.     public ThreadPoolExecutor(int corePoolSize,  
  136.                               int maximumPoolSize,  
  137.                               long keepAliveTime,  
  138.                               TimeUnit unit,  
  139.                               BlockingQueue<Runnable> workQueue,  
  140.                               ThreadFactory threadFactory,  
  141.                               RejectedExecutionHandler handler) {  
  142.         if (corePoolSize < 0 ||  
  143.             maximumPoolSize <= 0 ||  
  144.             maximumPoolSize < corePoolSize ||  
  145.             keepAliveTime < 0)  
  146.             throw new IllegalArgumentException();  
  147.         if (workQueue == null || threadFactory == null || handler == null)  
  148.             throw new NullPointerException();  
  149.         this.corePoolSize = corePoolSize;  
  150.         this.maximumPoolSize = maximumPoolSize;  
  151.         this.workQueue = workQueue;  
  152.         this.keepAliveTime = unit.toNanos(keepAliveTime);  
  153.         this.threadFactory = threadFactory;  
  154.         this.handler = handler;  
  155.     }  

    通过代码我们发现,ThreadPoolExecutor继承了AbstractExecutorService类继承了AbstractExecutorService类,并且提供了四个构造方法,大家看完就会发现,其实前面的三个构造方法调用的都是第四个构造方法进行的初始化工作。

 

    我们接下来看下构造器中各个参数的定义,结合jdk-api加入自己的理解:

 

    corePoolSize- 池中所保存的核心线程数,包括空闲线程,默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中;

 

    maximumPoolSize -池中允许的最大线程数。

 

    keepAliveTime- 当线程数大于核心时,线程没有任务执行时最多保持多久时间就会终止,默认情况下,只有当线程池中的线程数大于corePoolSize时,keepAliveTime才会起作用;如果线程池中的线程数小于corePoolSize,一个线程空闲的时间达到keepAliveTime,就会终止

 

    unit -参数keepAliveTime的时间单位,在TimeUnit类中有有七种静态属性:

[java]  view plain  copy
  1. TimeUnit.DAYS;               //天  
  2. TimeUnit.HOURS;             //小时  
  3. TimeUnit.MINUTES;           //分钟  
  4. TimeUnit.SECONDS;           //秒  
  5. TimeUnit.MILLISECONDS;      //毫秒  
  6. TimeUnit.MICROSECONDS;      //微妙  
  7. TimeUnit.NANOSECONDS;       //纳秒  

    workQueue- 执行前用于保持任务的队列 ,也就是用来存储等待执行的任务 。此队列仅保持由 execute 方法提交的 Runnable 任务。 一般我们也称它为阻塞队列,这个参数的选择也很重要,会对线程池的运行过程产生重大影响,关于阻塞队列的介绍我们放到下篇文章;

 

    threadFactory -执行程序创建新线程时使用的工厂;


    handler -由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序,常见有以下几种:

[java]  view plain  copy
  1.  ThreadPoolExecutor.AbortPolicy:放弃任务抛异常。  
  2. ThreadPoolExecutor.DiscardPolicy:放弃任务不抛异常。  
  3. ThreadPoolExecutor.DiscardOldestPolicy:放弃队列最前面的任务,然后重新尝试执行任务(重复此过程)  
  4. ThreadPoolExecutor.CallerRunsPolicy:调用线程处理该任务  

    通过代码我们发现,我们的ThreadPoolExecutor类继承了AbstractExecutorService抽象类,我们接下来看下AbstractExecutorService抽象类 :

 

[java]  view plain  copy
  1. public abstractclass AbstractExecutorService implements ExecutorService {  
  2. …………………………………………………..  

    关于AbstractExecutorService类的详细代码我们就不再展示了,它实现了ExecutorService接口,提供ExecutorService 执行方法的默认实现。此类使用 newTaskFor 返回的 RunnableFuture实现 submit、invokeAny和 invokeAll 方法。

 

    我们接着来看ExecutorService接口:

[java]  view plain  copy
  1. public interfaceExecutorService extends Executor {  
  2. …………………………………………………..  
  3.    

    ExecutorService接口又继承了 Executor 接口,ExecutorService接口是真正的线程池接口,ExecutorService是可以关闭的,这将导致其拒绝新任务。提供两个方法来关闭ExecutorService。shutdown()方法在终止前允许执行以前提交的任务,而 shutdownNow()方法阻止等待任务启动并试图停止当前正在执行的任务。在终止时,执行程序没有任务在执行,也没有任务在等待执行,并且无法提交新任务。应该关闭未使用的ExecutorService 以允许回收其资源。

 

    ExecutorService还可以通过创建并返回一个可用于取消执行和/或等待完成的Future,方法 submit 扩展了基本方法 Executor.execute(java.lang.Runnable)。方法 invokeAny 和invokeAll 是批量执行的最常用形式,它们执行任务 collection,然后等待至少一个,或全部任务完成(可使用ExecutorCompletionService 类来编写这些方法的自定义变体)。

 

    接下来看Executor接口

[java]  view plain  copy
  1. public interface Executor {  
  2.   
  3.     /** 
  4.      * Executes the given command at some time in the future.  The command 
  5.      * may execute in a new thread, in a pooled thread, or in the calling 
  6.      * thread, at the discretion of the {@code Executor} implementation. 
  7.      * 
  8.      * @param command the runnable task 
  9.      * @throws RejectedExecutionException if this task cannot be 
  10.      * accepted for execution 
  11.      * @throws NullPointerException if command is null 
  12.      */  
  13.     void execute(Runnable command);  
  14. }  

         这个接口只有一个 execute 方法,执行已提交的Runnable 任务的对象,所以 Executor  接口并不是一个线程池只是一个执行线程的工具。

 

 

    到这里大家应该明白ThreadPoolExecutor、AbstractExecutorService、ExecutorService和Executor几个之间的关系了Executor是最顶层的接口,是来执行线程任务的;ExecutorService接口继承了Executor接口,并声明了一些方法:submit、invokeAll、invokeAny以及shutDown等;抽象类AbstractExecutorService实现了ExecutorService接口,基本实现了ExecutorService中声明的所有方法;最后我们的ThreadPoolExecutor继承了类AbstractExecutorService。

 

    在我们的ThreadPoolExecutor类中有几个常用的方法:

execute()、submit()、shutdown()、shutdownNow(),其他方法大家可以去查api

 

    其中execute()方法实际上是Executor中声明的方法,在ThreadPoolExecutor进行了具体的实现,这个方法是ThreadPoolExecutor的核心方法,通过这个方法可以向线程池提交一个任务,交由线程池去执行。

 

    submit()方法是在ExecutorService中声明的方法,在AbstractExecutorService就已经有了具体的实现,在ThreadPoolExecutor中并没有对其进行重写,这个方法也是用来向线程池提交任务的,但是它和execute()方法不同,它能够返回任务执行的结果,去看submit()方法的实现,会发现它实际上还是调用的execute()方法,只不过它利用了Future来获取任务执行结果。

 

    shutdown()和shutdownNow()比较简单是用来关闭线程的。

 

总结

 

    关于线程池ThreadPoolExecutor我们本篇文章讲解了线程池是什么以及使用线程池的好处,并且通过一个简单的demo来让大家对线程池有一个整体认识,最后介绍了线程池ThreadPoolExecutor类的与父类以及接口的关系,关于ThreadPoolExecutor的介绍就先到这里。

 

博客来源:http://blog.csdn.net/zwk626542417/article/details/47084925

猜你喜欢

转载自blog.csdn.net/m_jack/article/details/80475789