使用场景:如定时任务批处理时,将分批任务用多线程进行处理,生成者主线程会快速创建任务,消费者无法及时处理完,希望生产者等待消费者消费后继续添加任务。
背景:当线程池任务满时,线程池的4种默认策略都是不阻塞的生产线程的,当
new ThreadPoolExecutor.CallerRunsPolicy() // 表示将提交的任务交给提交的人让它自己租处理。
new ThreadPoolExecutor.AbortPolicy() // 表示用抛异常的方式来拒绝执行提交的任务。
new ThreadPoolExecutor.DiscardPolicy() // 表示,直接把提交的任务抛弃掉.
new ThreadPoolExecutor.DiscardOldestPolicy() //表示丢弃掉时间最久的任务,并执行当前的任务。
关键点:自定义阻塞策略
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (!executor.isShutdown()) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}
完整的测试代码
package com.yuanba.test3.code;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
public class BlockThreadPoolTest {
public static void main(String[] args) {
ExecutorService threadPool = buildThreadPool();
for (int i=0; i<50; i++) {
threadPool.submit(() -> {
try {
Thread.sleep(1000);
System.out.println("thread:"+Thread.currentThread().getId());
} catch (Exception e) {
e.printStackTrace();
}
});
System.out.println("main -> i: "+i);
}
}
public static ExecutorService buildThreadPool() {
ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("demo-thread-%d").build();
return new ThreadPoolExecutor(2, 2,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(20), factory,
(r, executor) -> {
if (!executor.isShutdown()) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
);
}
}
测试代码简单说明:
- 主线程创建阻塞线程池
- 主线程总共要执行50个任务
- 每次任务加入线程池后输出任务编号
- 当线程池满时阻塞主线程,等待子线程完成
- 子线程完成会打印子线程线程id
运行结果:
main -> i: 0 main -> i: 1 main -> i: 2 main -> i: 3 main -> i: 4 main -> i: 5 main -> i: 6 main -> i: 7 main -> i: 8 main -> i: 9 main -> i: 10 main -> i: 11 main -> i: 12 main -> i: 13 main -> i: 14 main -> i: 15 main -> i: 16 main -> i: 17 main -> i: 18 main -> i: 19 main -> i: 20 main -> i: 21 thread:15 thread:16 main -> i: 22 main -> i: 23 thread:15 main -> i: 24 thread:16 main -> i: 25 thread:15 main -> i: 26 thread:16 main -> i: 27 thread:15 main -> i: 28 thread:16 main -> i: 29 thread:15 main -> i: 30 thread:16 main -> i: 31 thread:15 main -> i: 32 thread:16 main -> i: 33 thread:15 thread:16 main -> i: 34 main -> i: 35 thread:16 thread:15 main -> i: 36 main -> i: 37 thread:15 main -> i: 38 thread:16 main -> i: 39 thread:15 main -> i: 40 thread:16 main -> i: 41 thread:15 main -> i: 42 thread:16 main -> i: 43 thread:16 thread:15 main -> i: 44 main -> i: 45 thread:16 main -> i: 46 thread:15 main -> i: 47 thread:16 thread:15 main -> i: 48 main -> i: 49 thread:16 thread:15 thread:16 thread:15 thread:16 thread:15 thread:16 thread:15 thread:16 thread:15 thread:16 thread:15 thread:15 thread:16 thread:15 thread:16 thread:15 thread:16 thread:15 thread:16 thread:16 thread:15
结果分析:
- 这个线程池核心线程和最大线程数都是2,阻塞队列线程数是20,最多能容纳22个任务,所以最先的22个任务很快被加入了线程池,因此输出了0-21
- 而第23个任务因为线程池满,所以被阻塞
- 直到至少有1个子线程完成任务,所以这里会优先有子线程输出子线程id
- 而我们确实看到线程15和线程16优先输出了
- 然后2个新任务被加入线程池,看到主线程输出了22和23
- 后面的过程就是不停重复上面的多过程,直到所有任务跑完



