How does maximumPoolSize of ThreadPoolExecutor works?

后端 未结 3 856
渐次进展
渐次进展 2021-01-12 03:05

I\'m trying to understand ThreadPoolExecutor class. I have read this answer and the Javadoc. But my experimentation doesn\'t match with that description:

I initializ

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 03:24

    To make ThreadPool create new additional thread (to extend pool size according to maximumPoolSize parameter) try to execute this simple example:

    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            ThreadPoolExecutor tpe = new ThreadPoolExecutor(
                    1, 2, 500, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<>(1));
            System.out.println("init pool size= " + tpe.getPoolSize() + ", queue size=" + tpe.getQueue().size());
    
            tpe.execute(new Task("1st", 10000));
            Thread.sleep(1000);
            print(tpe, "1st");
    
            tpe.execute(new Task("2nd", 0));
            Thread.sleep(1000);
            print(tpe, "2nd");
    
            tpe.execute(new Task("3d", 2000));
            Thread.sleep(1000);
            print(tpe, "3d");
    
            while (tpe.getPoolSize()>1) {           
                Thread.sleep(100);
            }
            System.out.println("pool size= " + tpe.getPoolSize() + ", queue size=" + tpe.getQueue().size());
            tpe.shutdown();
        }
    
        private static void print(ThreadPoolExecutor tpe, String name) {
            System.out.println("After " + name + " execute -  pool size= " + tpe.getPoolSize() + ", queue=" + tpe.getQueue());
        }
    
        private static class Task implements Runnable {
    
            private final String name;
            private final long time;
    
            Task(String name, long time) {
                this.name = name;
                this.time = time;
            }
    
            @Override
            public void run() {
                System.out.println("Run " + Thread.currentThread().getName() + "-" + name);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
                System.out.println("Finish " + Thread.currentThread().getName() + "-" + name);
            }
    
            @Override
            public String toString() {
                return name;
            }
    
        }
    }
    

    You'll get the output which demonstrates impact of maximumPoolSize and keepAliveTime:

    init pool size= 0, queue size=0
    Run pool-1-thread-1-1st
    After 1st execute -  pool size= 1, queue=[]
    After 2nd execute -  pool size= 1, queue=[2nd]
    Run pool-1-thread-2-3d
    After 3d execute -  pool size= 2, queue=[2nd]
    Finish pool-1-thread-2-3d
    Run pool-1-thread-2-2nd
    Finish pool-1-thread-2-2nd
    pool size= 1, queue size=0
    Finish pool-1-thread-1-1st
    

提交回复
热议问题