import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Tests {
private static final AtomicInteger COUNTER = new AtomicInteger(1);
private static final AtomicInteger THREAD_EXPAND = new AtomicInteger(1);
// 阻塞隊列一旦滿了會拋出異常
private static final LinkedBlockingQueue QUEUE = new LinkedBlockingQueue(30);
public static void main(String[] args) {
int corePoolSize = 4;
int maximumPoolSize = 10;
// 線程池第二個參數是線程組大數量
// 只有在WORK QUEUE滿了之后才會擴容
// 如果WORK QUEUE滿進行過擴容之后依舊有任務追加則會執行RejectedExecutionHandler策略
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 1000L, TimeUnit.MILLISECONDS, QUEUE, new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println("rejectedExecution $: 此處隊列大小:" + QUEUE.size() + " 此時ActiveCount: " + executor.getActiveCount() + " 拒絕次數: " + COUNTER.getAndIncrement());
}
});
for (int i = 1; i <= 50; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println("當前任務id :" + i + " => 此時ActiveCount:" + executor.getActiveCount());
if (executor.getActiveCount() > corePoolSize && executor.getActiveCount() < maximumPoolSize) {
System.out.println("線程池進行擴容:" + THREAD_EXPAND.getAndIncrement() + " 共計活躍線程數:" + executor.getActiveCount());
}
}
}
}
// output:
// 當前任務id :1 => 此時ActiveCount:1
// 當前任務id :2 => 此時ActiveCount:2
// 當前任務id :3 => 此時ActiveCount:3
// 當前任務id :4 => 此時ActiveCount:4
// 當前任務id :5 => 此時ActiveCount:4
// 當前任務id :6 => 此時ActiveCount:4
// 當前任務id :7 => 此時ActiveCount:4
// 當前任務id :8 => 此時ActiveCount:4
// 當前任務id :9 => 此時ActiveCount:4
// 當前任務id :10 => 此時ActiveCount:4
// 當前任務id :11 => 此時ActiveCount:4
// 當前任務id :12 => 此時ActiveCount:4
// 當前任務id :13 => 此時ActiveCount:4
// 當前任務id :14 => 此時ActiveCount:4
// 當前任務id :15 => 此時ActiveCount:4
// 當前任務id :16 => 此時ActiveCount:4
// 當前任務id :17 => 此時ActiveCount:4
// 當前任務id :18 => 此時ActiveCount:4
// 當前任務id :19 => 此時ActiveCount:4
// 當前任務id :20 => 此時ActiveCount:4
// 當前任務id :21 => 此時ActiveCount:4
// 當前任務id :22 => 此時ActiveCount:4
// 當前任務id :23 => 此時ActiveCount:4
// 當前任務id :24 => 此時ActiveCount:4
// 當前任務id :25 => 此時ActiveCount:4
// 當前任務id :26 => 此時ActiveCount:4
// 當前任務id :27 => 此時ActiveCount:4
// 當前任務id :28 => 此時ActiveCount:4
// 當前任務id :29 => 此時ActiveCount:4
// 當前任務id :30 => 此時ActiveCount:4
// 當前任務id :31 => 此時ActiveCount:4
// 當前任務id :32 => 此時ActiveCount:4
// 當前任務id :33 => 此時ActiveCount:4
// 當前任務id :34 => 此時ActiveCount:4
// 當前任務id :35 => 此時ActiveCount:5
// 線程池進行擴容:1 共計活躍線程數:5
// 當前任務id :36 => 此時ActiveCount:6
// 線程池進行擴容:2 共計活躍線程數:6
// 當前任務id :37 => 此時ActiveCount:7
// 線程池進行擴容:3 共計活躍線程數:7
// 當前任務id :38 => 此時ActiveCount:8
// 線程池進行擴容:4 共計活躍線程數:8
// 當前任務id :39 => 此時ActiveCount:9
// 線程池進行擴容:5 共計活躍線程數:9
// 當前任務id :40 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 1
// 當前任務id :41 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 2
// 當前任務id :42 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 3
// 當前任務id :43 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 4
// 當前任務id :44 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 5
// 當前任務id :45 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 6
// 當前任務id :46 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 7
// 當前任務id :47 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 8
// 當前任務id :48 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 9
// 當前任務id :49 => 此時ActiveCount:10
// rejectedExecution $: 此處隊列大小:30 此時ActiveCount: 10 拒絕次數: 10
// 當前任務id :50 => 此時ActiveCount:10