好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java线程池对象ThreadPoolExecutor的深入讲解

使用线程池的好处

1、降低资源消耗

可以重复利用已创建的线程降低线程创建和销毁造成的消耗。

2、提高响应速度

当任务到达时,任务可以不需要等到线程创建就能立即执行。

3、提高线程的可管理性

线程是稀缺资源,如果无限制地创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一分配、调优和监控

threadpoolexecutor 介绍:

java 提供的线程池类;

threadpoolexecutor 作用:

两个作用:

1,用于分离执行任务和当前线程;

2,主要设计初衷:重复利用thread 对象;

threadpoolexecutor 使用:

实例化:

?

1

2

3

4

5

6

7

8

public threadpoolexecutor( int corepoolsize,

         int maximumpoolsize,

         long keepalivetime,

         timeunit unit,

         blockingqueue<runnable> workqueue) {

   this (corepoolsize, maximumpoolsize, keepalivetime, unit, workqueue,

     executors.defaultthreadfactory(), defaulthandler);

  }

这是threadpoolexecutor的构造方法之一,传入参数最少,以下是参数说明:

corepoolsize  : 设置线程池的线程核心数量;该参数作用:当向线程池中添加的任务数量小于核心数量时,线程池将优先创建新的线程,而不是重用之前已存在的线程(不管该线程可用与否);

源码佐证,threadpoolexecutor 的 execute(runnable) 方法中有源码如下:

maximumpoolsize : 设置线程池可创建最大线程数量;该参数作用:用于限制线程池无限制的创建线程;

源码佐证,threadpoolexecutor 的 addworker(runnable ,boolean) 方法中有源码如下:

keepalivetime  : 设置线程池被创建线程的存活时间;

源码佐证,threadpoolexecutor 的 gettask()方法中有源码如下;源码说明:创建的线程将从任务队列中获取一个新的任务,在keepalivetime时间之后如果还未获取到任务,将关闭该线程;

 

unit :设置线程池线程存活时间的时间单位;

workqueue :设置线程池用于存放任务的队列;

注意: threadpoolexecutor线程池是否创建新的线程不仅仅依赖于corepoolsize变量,还依赖于任务队列的offer(e) 方法所返回的值;比如:想要创建一个cache线程池,就依赖于一个特殊的任务队列:synchronousqueue<e>;

源码佐证,threadpoolexecutor 的execute(runnable) 方法中有源码如下:

示例: 创建固定线程池 和 cache线程池;

固定线程池,将corepoolsize 和 maximumpoolsize 设置相同即可,在executors.newfixedthreadpool(10)有源码示例:

?

1

2

3

4

5

public static executorservice newfixedthreadpool( int nthreads) {

  return new threadpoolexecutor(nthreads, nthreads,

          0l, timeunit.milliseconds,

          new linkedblockingqueue<runnable>());

}

cacahe线程池,需要传入特殊的blockingqueue对象,该对象需要在offer是返回false,并能够在poll方法中监听到offer进入的任务;java 提供了一个synchronousqueue类,该类就是这样一个对象;在executors.newcachedthreadpool()方法中,有源码示例:

?

1

2

3

4

5

public static executorservice newcachedthreadpool() {

  return new threadpoolexecutor( 0 , integer.max_value,

          60l, timeunit.seconds,

          new synchronousqueue<runnable>());

}

常用方法提示:

  void execute(runnable command) : 任务提交方法;线程池将顺序执行所提交的所有方法;

  void shutdown() : 停止后续任务提交,但执行完当前线程池中所有任务;该方法的作用:线程池将中断当前所有空闲的线程,但不保证一定中断(可查看thread的interrupt方法);那么工作中的线程将继续工作,直到完成;

源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public void shutdown() {

   final reentrantlock mainlock = this .mainlock;

   mainlock.lock();

   try {

    checkshutdownaccess();

    advancerunstate(shutdown);

    interruptidleworkers(); 、 //中断所有空闲的线程

    onshutdown(); // hook for scheduledthreadpoolexecutor

   } finally {

    mainlock.unlock();

   }

   tryterminate();

  }

  list<runnable> shutdownnow() : 立即终止线程池;该方法的作用:线程池将中断所有创建的线程,但不保存一定中断;线程池将所有剩余的任务从任务队列中移除,不在执行,并保存在一个list中返回给用于;

方法源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public list<runnable> shutdownnow() {

   list<runnable> tasks;

   final reentrantlock mainlock = this .mainlock;

   mainlock.lock();

   try {

    checkshutdownaccess();

    advancerunstate(stop);

    interruptworkers();

    tasks = drainqueue();

   } finally {

    mainlock.unlock();

   }

   tryterminate();

   return tasks;

  }

threadpoolexecutor 任务执行流程图:

  

threadpoolexecutor 使用注意点说明:

清晰需要使用的是那种类型的线程池,在实例化threadpoolexecutor时,传入的参数不同创建出来的线程池也将不同;尤其注意传入blockingqueue参数,如果需要使用cache线程池,请确保blockingqueue的offer方法反回false;可以参见synchronousqueue类;

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:http://www.cnblogs.com/loveyoumi/p/9578251.html

查看更多关于java线程池对象ThreadPoolExecutor的深入讲解的详细内容...

  阅读:42次