package com.example.demo.test.ThreadPool;
import java.util.concurrent.*;
public class ThreadPoolTest {
// 核心线程池大小
private int corePoolSize = 1;
// 最大可创建的线程数
private int maxPoolSize = 3;
// 队列最大长度
private int queueCapacity = 3;
// 线程池维护线程所允许的空闲时间
private int keepAliveSeconds = 30;
private ThreadPoolExecutor getExcutor(){
BlockingDeque<Runnable> blockingDeque = new LinkedBlockingDeque<Runnable>(queueCapacity);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maxPoolSize,keepAliveSeconds, TimeUnit.SECONDS,blockingDeque,new ThreadPoolExecutor.CallerRunsPolicy());
return threadPoolExecutor;
}
public static void main(String[] args) {
ThreadPoolTest threadPoolTest = new ThreadPoolTest();
ThreadPoolExecutor threadPoolExecutor = threadPoolTest.getExcutor();
final Integer m = 0;
for(int i = 0;i<10;i++){
int finalI = i;
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
System.out.println("come in"+ finalI);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("come out"+ finalI);
}
});
}
threadPoolExecutor.shutdown();
}
}
结果:
come in0
come in4
come in5
come in6
come out0
come out4
come out5
come out6
come in1
come in3
come in2
come out1
come out2
come out3
come in8
come in7
come in9
come out7
come out9
come out8
不多废话,拒绝策略的影响自己测试