ThreadPoolExecutor参数

时间:2017-12-06 12:05:28   收藏:0   阅读:333

 

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、我对参数的理解

 

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

 

原文:http://www.cnblogs.com/huanyou/p/7988621.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!