public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
ThreadPoolExecutor其他常见参数:
package ThreadPoolExecutorDemo;
import java.util.Date;
//创建一个简单的Runnable类,需要大约5s中来执行其任务
public class MyRunnable implements Runnable{
private String command;
public MyRunnable(String s){
this.command = s;
}
@Override
public void run(){
System.out.println(Thread.currentThread().getName() + " start. Time = " + new Date());
processCommand();
System.out.println(Thread.currentThread().getName() + " End. Time = " + new Date());
}
private void processCommand(){
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
@Override
public String toString(){
return this.command;
}
}
corePoolSize: 核心线程数为 5。
maximumPoolSize :最大线程数 10
keepAliveTime : 等待时间为 1L。
unit: 等待时间的单位为 TimeUnit.SECONDS。
workQueue:任务队列为 ArrayBlockingQueue,并且容量为 100;
handler:饱和策略为 CallerRunsPolicy。
package ThreadPoolExecutorDemo;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolExecutorDemo {
private static final int CORE_POOL_SIZE = 5;
private static final int MAX_POOL_SIZE = 10;
private static final int QUEUE_CAPACITY = 100;
private static final Long KEEP_ALIVE_TIME = 1L;
public static void main(String[] args){
//
//
ThreadPoolExecutor executor = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(QUEUE_CAPACITY),
new ThreadPoolExecutor.CallerRunsPolicy()
);
for(int i = 0; i < 10; i++){
Runnable worker = new MyRunnable("" + i);
executor.execute(worker);
}
//
executor.shutdown();
while(!executor.isTerminated()){
}
System.out.println("Finished all threads");
}
}
pool-1-thread-1 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-3 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-4 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-2 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-5 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-1 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-3 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-4 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-5 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-2 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-5 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-4 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-3 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-1 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-2 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-3 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-1 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-5 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-4 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-2 End. Time = Thu Aug 18 10:20:40 CST 2022
Finished all threads

在代码中模拟了10个任务,配置的核心线程数为5,等待队列容量为100,所以每次只可能存在5个任务同时执行,剩下的5个任务会被放到等待队列中。当前的5个任务中如果有任务被执行完了,现成就会去拿新的任务去执行。