为了对Java多线程编程有一个更深入的学习和了解,接下来会从Java线程池框架入手进行了解。作为开头,首先来了解一下,ExecuteThreadPool的一些属性。

1、线程池状态,在JDK原码注释中,讲得很清楚,在这里我直接将其Copy过来

/**
 * runState provides the main lifecyle control, taking on values:
 *
 *   RUNNING:  Accept new tasks and process queued tasks
 *   SHUTDOWN: Don't accept new tasks, but process queued tasks
 *   STOP:     Don't accept new tasks, don't process queued tasks,
 *             and interrupt in-progress tasks
 *   TERMINATED: Same as STOP, plus all threads have terminated
 *
 * The numerical order among these values matters, to allow
 * ordered comparisons. The runState monotonically increases over
 * time, but need not hit each state. The transitions are:
 *
 * RUNNING -> SHUTDOWN
 *    On invocation of shutdown(), perhaps implicitly in finalize()
 * (RUNNING or SHUTDOWN) -> STOP
 *    On invocation of shutdownNow()
 * SHUTDOWN -> TERMINATED
 *    When both queue and pool are empty
 * STOP -> TERMINATED
 *    When pool is empty
 */

 

2、当使用tpe=(ThreadPoolExecutor)Executors.newCachedThreadPool()

//池中当前线程的数量,默认满1分钟后,线程没有变化,则会被清除
System.out.println("-----PoolSize---------:"+tpe.getPoolSize());
 //池中,保持激活的最小work数量,默认为0
System.out.println("-----CorePoolSize-----:"+tpe.getCorePoolSize());
//在池中同时出现的,最大线程数量
System.out.println("----LargestPoolSize---:"+tpe.getLargestPoolSize());
//线程池允许的最大线程数量,如果没有设置则为int的取值最大范围
 System.out.println("-----MaximumPoolSize--:"+tpe.getMaximumPoolSize());
//对已提交的一个大概的任务数量
 System.out.println("-----TaskCount--------:"+tpe.getTaskCount());
//返回被激活的,正在执行的任务数量
System.out.println("-----ActiveCount------:"+tpe.getActiveCount());
//执行完的任务数量
System.out.println("--CompletedTaskCount--:"+tpe.getCompletedTaskCount());

//始终为0
System.out.println("-----Queue Size-------:"+tpe.getQueue().size());

 

3、当使用tpe=(ThreadPoolExecutor)(ThreadPoolExecutor) Executors.newFixedThreadPool(n)

//池中当前线程的数量,默认激活为n,当用户提交的任务被执行完以后,对应的任务会变成idleWorker,只有执行shutdownNow()时,才会将其中断。
System.out.println("-----PoolSize---------:"+tpe.getPoolSize());
 //池中,保持激活的最小work数量,默认为n
System.out.println("-----CorePoolSize-----:"+tpe.getCorePoolSize());
//在池中同时出现的,最大线程数量n
System.out.println("----LargestPoolSize---:"+tpe.getLargestPoolSize());
//线程池允许的最大线程数量n
 System.out.println("-----MaximumPoolSize--:"+tpe.getMaximumPoolSize());
//对已提交的一个大概的任务数量
 System.out.println("-----TaskCount--------:"+tpe.getTaskCount());
//返回被激活的,正在执行的任务数量
System.out.println("-----ActiveCount------:"+tpe.getActiveCount());
//执行完的任务数量
System.out.println("--CompletedTaskCount--:"+tpe.getCompletedTaskCount());

//处于等待执行的任务,会被放在队列当中
System.out.println("-----Queue Size-------:"+tpe.getQueue().size());

 

4、对idleWorker和Queue Size的补充

(1)已提交的任务,指通过调用线程池执行器的execute时传入的任务,如果使用的是 Executes.newCachedThreadPool(),所有提交的任务都会被放在workers对象中,而其对应的workQueue始终为 0,当提交的任务数超过MaximumPoolSize时,会抛出相应的异常。如果使用的是Executes.newFixedThreadPool(n),所有提交的任务,首先会被放入workers当中,当提交的任务数量大于corePoolSize的时候,其他的任务会被放在Queue当中。

(2)已提交并获取锁的任务(激活的任务),会被绑定在一个worker上,worker相当于是一个工作者,对绑定的线程负责,同时还向线程池反馈线程的信息。 而这些worker会被添加到一个名为workers的HashSet集合当中,被重复的运用。如果一个线程执行完,并且又没有新的线程添加进来 时,worker就变成了idleWorker(工作线程,没有什么特别的用处),如果有新的进程添加进来,则又绑定到了Worker上面。

posted on 2014-09-24 13:34  天狼群星  阅读(2031)  评论(0)    收藏  举报