面试复习方便, 个人学习笔记
public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() { }
// 直接抛出异常
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() { }
// 线程池资源用完了, 调用者自己去执行
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() { }
// 啥也不干, 也是一种策略
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() { }
// 最旧的抛弃掉
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
/**
* 线程池学习
*
* @author xiaozhengN 571457082@qq.com
* @since 2022-10-19 22:39:13
**/
@Slf4j
public class TestThreadPoolExecutor {
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger(1);
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
2,
3,
0,
TimeUnit.MICROSECONDS,
new ArrayBlockingQueue<>(2),
r -> new Thread(r, "myThread" + atomicInteger.getAndIncrement()),
new ThreadPoolExecutor.DiscardOldestPolicy()
);
showState(threadPool);
threadPool.submit(new MyTask("1", 3600000));
showState(threadPool);
threadPool.submit(new MyTask("2", 3600000));
showState(threadPool);
threadPool.submit(new MyTask("3"));
showState(threadPool);
threadPool.submit(new MyTask("4"));
showState(threadPool);
threadPool.submit(new MyTask("5", 3600000));
showState(threadPool);
threadPool.submit(new MyTask("6"));
showState(threadPool);
}
// 获取线程池核心线程数和阻塞队列信息
private static void showState(ThreadPoolExecutor threadPool) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<Object> blockingQueueList = new ArrayList<>();
for (Runnable runnable : threadPool.getQueue()) {
try {
Field callable = FutureTask.class.getDeclaredField("callable");
callable.setAccessible(true);
Object adapter = callable.get(runnable);
Class<?> clazz = Class.forName("java.util.concurrent.Executors$RunnableAdapter");
Field task = clazz.getDeclaredField("task");
task.setAccessible(true);
Object o = task.get(adapter);
blockingQueueList.add(o);
} catch (Exception e) {
e.printStackTrace();
}
}
log.debug("pool size: {}, queue: {}", threadPool.getPoolSize(), blockingQueueList);
}
static class MyTask implements Runnable {
private final String name;
private final long duration;
public MyTask(String name) {
this(name, 0);
}
public MyTask(String name, long duration) {
this.name = name;
this.duration = duration;
}
@Override
public void run() {
try {
log.debug("running...name: {}, obj: {}", Thread.currentThread().getName(), this);
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "MyTask(" + name + ")";
}
}
}
日志:
2022-10-19 23:39:25,336 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 0, queue: []
2022-10-19 23:39:25,337 DEBUG [main.daily.TestThreadPoolExecutor] - running...name: myThread1, obj: MyTask(1)
2022-10-19 23:39:25,637 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 1, queue: []
2022-10-19 23:39:25,637 DEBUG [main.daily.TestThreadPoolExecutor] - running...name: myThread2, obj: MyTask(2)
2022-10-19 23:39:25,937 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 2, queue: []
2022-10-19 23:39:26,238 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 2, queue: [MyTask(3)]
2022-10-19 23:39:26,538 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 2, queue: [MyTask(3), MyTask(4)]
2022-10-19 23:39:26,538 DEBUG [main.daily.TestThreadPoolExecutor] - running...name: myThread3, obj: MyTask(5)
2022-10-19 23:39:26,839 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 3, queue: [MyTask(3), MyTask(4)]
2022-10-19 23:39:27,139 DEBUG [main.daily.TestThreadPoolExecutor] - pool size: 3, queue: [MyTask(4), MyTask(6)]