1、ThreadPoolExecutor个参数的意义(类上的注释内容)
* @param corePoolSize the number of threads to keep in the
* pool, even if they are idle.
* @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 keepAliveTime
* argument.
* @param workQueue the queue to use for holding tasks before they
* are executed. This queue will hold only the <tt>Runnable</tt>
* tasks submitted by the <tt>execute</tt> 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.
2、我对参数的理解(重要)
- corePoolSize:核心线程数,线程池创建时,就会创建的线程数。所有线程都执行完后,核心线程依然会保持。
- maximumPoolSize:最大的线程数。线程池最多会初始化maximumPoolSize个线程。
- keepAliveTime:idle线程存活时间。
- unit:keepAliveTime对应的时间单位。
- workQueue:队列,如果任务数已经达到corePoolSize,再向线程池中加入任务时,会将任务放到workQueue中。可以使用LinkedBlockingQueue,队列的数量没有限制。在构造BlockingQueue的时候,也可以传入最大值。如果有最大值,在queue中任务的数量达到最大值,再向线程池中加入任务时,就会启动新的线程,直到线程池中线程的数量达到maximumPoolSize为止。再向线程池中加入线程,会报错。
- threadFactory:新线程的构造器。
- handler:如果抛出异常,异常处理的Handler。
综上:线程池中的线程数量在线程池初始化的时候,为corePoolSize;随着加入线程池的任务数量增加, 运行的线程并不会增加,无法运行的任务会放到workQueue队列中;当workQueue队列已经放满了任务,新加入的任务会再启动新的线程来执行,直到正在运行的线程数量达到maximumPoolSize为止;再向线程池中添加任务,将会抛出异常。
3、验证各参数的代码
public static class ExecutorHelper { public static String getInfo(ThreadPoolExecutor executor) { StringBuilder sb = new StringBuilder(); sb.append("************** corePoolSize: " + executor.getCorePoolSize()); sb.append("************** poolSize: " + executor.getPoolSize()); sb.append("************** activeCount: " + executor.getActiveCount()); sb.append("************** completeTaskCount: " + executor.getCompletedTaskCount()); sb.append("************** largestPoolSize: " + executor.getLargestPoolSize()); sb.append("************** maximumPoolSize: " + executor.getMaximumPoolSize()); sb.append("************** taskCount: " + executor.getTaskCount()); sb.append("************** queueSize: " + executor.getQueue().size()); // sb.append("************** corePoolSize: " + executor.getCorePoolSize()); // sb.append("************** corePoolSize: " + executor.getCorePoolSize()); return sb.toString(); } } public static class MyRunnable implements Runnable { private ThreadPoolExecutor executor; public MyRunnable(ThreadPoolExecutor executor) { this.executor = executor; System.out.println(ExecutorHelper.getInfo(executor)); } @Override public void run() { Thread th = Thread.currentThread(); System.out.println(th.getName()); // System.out.println(ExecutorHelper.getInfo(executor)); try { Thread.sleep(1000 * 30); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws Exception { ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) { Runnable runnable = new MyRunnable(executor); executor.submit(runnable); Thread.sleep(1000); } }
4、输出的内容:
************** corePoolSize: 2************** poolSize: 0************** activeCount: 0************** completeTaskCount: 0************** largestPoolSize: 0************** maximumPoolSize: 5************** taskCount: 0************** queueSize: 0 pool-2-thread-1 ************** corePoolSize: 2************** poolSize: 1************** activeCount: 1************** completeTaskCount: 0************** largestPoolSize: 1************** maximumPoolSize: 5************** taskCount: 1************** queueSize: 0 pool-2-thread-2 ************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 2************** queueSize: 0 ************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 3************** queueSize: 1 ************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 4************** queueSize: 2 ************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 5************** queueSize: 3 ************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 6************** queueSize: 4 pool-2-thread-3 ************** corePoolSize: 2************** poolSize: 3************** activeCount: 3************** completeTaskCount: 0************** largestPoolSize: 3************** maximumPoolSize: 5************** taskCount: 7************** queueSize: 4 pool-2-thread-4 ************** corePoolSize: 2************** poolSize: 4************** activeCount: 4************** completeTaskCount: 0************** largestPoolSize: 4************** maximumPoolSize: 5************** taskCount: 8************** queueSize: 4 pool-2-thread-5 Exception in thread "main" java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1768) ************** corePoolSize: 2************** poolSize: 5************** activeCount: 5************** completeTaskCount: 0************** largestPoolSize: 5************** maximumPoolSize: 5************** taskCount: 9************** queueSize: 4 at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:767) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:658) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:78) at com.jd.wms.inbound.service.socket.client.DhSocketClient.main(DhSocketClient.java:122) pool-2-thread-1 pool-2-thread-2 pool-2-thread-3 pool-2-thread-4