jdk提供了非常方便的方式来生成线程池, 如Executors.newxxxxxx的方式, 实现实际使用的都是以下ThreadPoolExecutor的方法:
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
线程池的管理是这样一个过程:
首先创建线程池, 然后根据任务的数量逐步将线程增大到corePoolSize数量, 如果此时任有任务增加, 则放置到workQueue中, 直到workQueue爆满为止, 然后继续增加线程池中的线程数量,增加处理能力,最终达到maximumPoolSize。 如果此时还是有任务增加进来会怎样呢 ? 这就需要handler来处理了,或者丢弃新任务, 或者拒绝新任务,或者挤占已有任务等。在任务队列何线程池都饱和情况下,一旦有线程处于等待(任务处理完毕, 没有新任务增加)状态的时间超过keepAliveTime,则该线程终止, 也就是说池中的线程数量会足部降低, 直至为corePoolSize数量为止。 至于threadFactory,可以自己新增一个,设置线程的自定义名称, daemon状态,便于后续排查错误。
我们用一个测试程序来帮助理解线程池的运行过程:
public class ThreadPoolTest {
private static ExecutorService es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(100000));
public static void main(String[] args) throws Exception {
for (int i = 0; i < 100000; i++) {
es.execute(() -> {
System.out.print(1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
ThreadPoolExecutor tpe = ((ThreadPoolExecutor) es);
while (true) {
System.out.println();
int queueSize = tpe.getQueue().size();
System.out.println(“当前排队任务数:” + queueSize);
int activeCount = tpe.getActiveCount();
System.out.println(“当前活动线程数:” + activeCount);
long completedTaskCount = tpe.getCompletedTaskCount();
System.out.println(“执行完成线程数:” + completedTaskCount);
long taskCount = tpe.getTaskCount();
System.out.println(“总线程数:” + taskCount);
Thread.sleep(3000);
}
}
}
程序每3秒打印一次线程池情况:
11111111111111111111111111111111111111111111111111
当前排队线程数:99950
当前活动线程数:50
执行完成线程数:0
总线程数:100000
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99849
当前活动线程数:50
执行完成线程数:127
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99700
当前活动线程数:50
执行完成线程数:250
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99550
当前活动线程数:50
执行完成线程数:400
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99400
当前活动线程数:50
执行完成线程数:550
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99250
当前活动线程数:50
执行完成线程数:700
总线程数:100000
。。。。。。
可以看到当前活动的线程数永远都是50, 为什么就没有突破?怎么没到100呢? 大家可以思考下,相信看了上面的描述,很容易就能理解了,DONE!
Sorry, the comment form is closed at this time.