package com.example.demo.threadpool;publicclassInternalTaskimplementsRunnable{privatefinal RunnableQueue runnableQueue;privatevolatileboolean running =true;publicInternalTask(RunnableQueue runnableQueue){this.runnableQueue = runnableQueue;}/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/@Overridepublicvoidrun(){while(running &&!Thread.currentThread().isInterrupted()){try{
Runnable task = runnableQueue.take();
task.run();}catch(InterruptedException e){
running =false;break;}}}/**
* 停止当前任务,主要会在线程池的 shutdown方法中使用
*/publicvoidstop(){this.running =false;}}
package com.example.demo.threadpool;import java.util.concurrent.TimeUnit;publicclassThreadPoolDemo{publicstaticvoidmain(String[] args)throws InterruptedException{final ThreadPool threadPool =newBasicThreadPool(2,6,4,1000);for(int i =0; i <20; i++){
threadPool.execute(()->{try{
TimeUnit.SECONDS.sleep(10);
System.out.println(Thread.currentThread().getName()+" is running and done.");}catch(InterruptedException e){
e.printStackTrace();}});}for(;;){
System.out.println("active count: "+ threadPool.getActiveCount());
System.out.println("queue size: "+ threadPool.getQueueSize());
System.out.println("core size: "+ threadPool.getCoreSize());
System.out.println("max size: "+ threadPool.getMaxSize());
System.out.println("===================================================");
TimeUnit.SECONDS.sleep(5);}}}
//result
active count:2
queue size:18
core size:4
max size:6===================================================
active count:2
queue size:18
core size:4
max size:6===================================================
active count:4
queue size:16
core size:4
max size:6===================================================
thread-pool-0 is running and done.
thread-pool-1 is running and done.
active count:4
queue size:14
core size:4
max size:6===================================================
thread-pool-2 is running and done.
thread-pool-3 is running and done.
thread-pool-1 is running and done.
active count:6
queue size:9
core size:4
max size:6
thread-pool-0 is running and done.===================================================
active count:6
queue size:8
core size:4
max size:6===================================================
thread-pool-3 is running and done.
thread-pool-4 is running and done.
thread-pool-5 is running and done.
thread-pool-2 is running and done.
thread-pool-1 is running and done.
thread-pool-0 is running and done.
active count:6
queue size:2
core size:4
max size:6===================================================
active count:6
queue size:2
core size:4
max size:6===================================================
thread-pool-2 is running and done.
thread-pool-4 is running and done.
thread-pool-3 is running and done.
thread-pool-5 is running and done.
thread-pool-1 is running and done.
thread-pool-0 is running and done.
active count:6
queue size:0
core size:4
max size:6===================================================
active count:6
queue size:0
core size:4
max size:6===================================================
thread-pool-2 is running and done.
thread-pool-4 is running and done.
active count:5
queue size:0
core size:4
max size:6===================================================
active count:5
queue size:0
core size:4
max size:6===================================================