the number of threads to keep in the pool, even if they are idle.
maximumPoolSize
the maximum number of threads to allow in the pool.
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.
unit
the time unit for the {@code keepAliveTime} argument
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.
publicclassTaskimplementsRunnable{privatefinalString name;publicTask(String name){this.name = name;}@Overridepublicvoidrun(){System.out.println(Thread.currentThread().getName()+" → "+ name +" Start Time = "+newDate());processCommand();System.out.println(Thread.currentThread().getName()+" → "+ name +" End Time = "+newDate());}privatevoidprocessCommand(){try{Thread.sleep(3000);}catch(InterruptedException e){
e.printStackTrace();}}}
自定义线程池
publicclassCustomThreadPool{publicstaticvoidmain(String[] args){System.out.println(Thread.currentThread().getName()+"线程: Start at: "+newDate());// 创建等待队列BlockingQueue<Runnable> blockingQueue =newArrayBlockingQueue<>(20);// 创建线程池、池中保存的线程数为3,允许的最大线程数为5ThreadPoolExecutor pool =newThreadPoolExecutor(3,5,50,TimeUnit.MILLISECONDS, blockingQueue);// 创建七个任务// 每个任务会在一个线程上执行for(int i =1; i <10; i++){System.out.println("添加了第"+ i +"个任务类");
pool.execute(newTask("线程任务"+ i));}// 关闭线程池
pool.shutdown();System.out.println(Thread.currentThread().getName()+"线程: 打卡:"+newDate());long i =0;while(!pool.isTerminated()){// wait for all tasks to finish
i++;}System.out.println(Thread.currentThread().getName()+"线程: Finished all threads at:"+newDate()+". isTerminated 判断次数 "+ i);}}
执行结果分析
执行结果
main线程: Start at: Fri May 19 18:28:52 CST 2023
进入main线程
添加了第1个任务类
构建任务放入线程池
添加了第2个任务类
添加了第3个任务类
添加了第4个任务类
添加了第5个任务类
添加了第6个任务类
添加了第7个任务类
添加了第8个任务类
添加了第9个任务类
添加了第10个任务类
main线程: 打卡:Fri May 19 18:28:52 CST 2023
主线程打个卡
pool-1-thread-2 → 线程任务2 Start Time = Fri May 19 18:28:52 CST 2023
线程池开始分配线程执行线程任务
pool-1-thread-1 → 线程任务1 Start Time = Fri May 19 18:28:52 CST 2023
pool-1-thread-3 → 线程任务3 Start Time = Fri May 19 18:28:52 CST 2023
pool-1-thread-2 → 线程任务2 End Time = Fri May 19 18:28:55 CST 2023
pool-1-thread-2 → 线程任务4 Start Time = Fri May 19 18:28:55 CST 2023
pool-1-thread-3 → 线程任务3 End Time = Fri May 19 18:28:55 CST 2023
pool-1-thread-1 → 线程任务1 End Time = Fri May 19 18:28:55 CST 2023
pool-1-thread-1 → 线程任务6 Start Time = Fri May 19 18:28:55 CST 2023
pool-1-thread-3 → 线程任务5 Start Time = Fri May 19 18:28:55 CST 2023
pool-1-thread-2 → 线程任务4 End Time = Fri May 19 18:28:58 CST 2023
pool-1-thread-2 → 线程任务7 Start Time = Fri May 19 18:28:58 CST 2023
pool-1-thread-3 → 线程任务5 End Time = Fri May 19 18:28:58 CST 2023
pool-1-thread-1 → 线程任务6 End Time = Fri May 19 18:28:58 CST 2023
pool-1-thread-3 → 线程任务8 Start Time = Fri May 19 18:28:58 CST 2023
pool-1-thread-1 → 线程任务9 Start Time = Fri May 19 18:28:58 CST 2023
pool-1-thread-2 → 线程任务7 End Time = Fri May 19 18:29:01 CST 2023
pool-1-thread-2 → 线程任务10 Start Time = Fri May 19 18:29:01 CST 2023
pool-1-thread-3 → 线程任务8 End Time = Fri May 19 18:29:01 CST 2023
pool-1-thread-1 → 线程任务9 End Time = Fri May 19 18:29:01 CST 2023
pool-1-thread-2 → 线程任务10 End Time = Fri May 19 18:29:04 CST 2023
main线程: Finished all threads at:Fri May 19 18:29:04 CST 2023. isTerminated 判断次数 25692088316