pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok pool-1-thread-1 ok
pool-1-thread-3 ok pool-1-thread-2 ok pool-1-thread-1 ok pool-1-thread-2 ok pool-1-thread-3 ok pool-1-thread-2 ok pool-1-thread-1 ok pool-1-thread-2 ok pool-1-thread-3 ok pool-1-thread-1 ok
pool-1-thread-4 ok pool-1-thread-3 ok pool-1-thread-1 ok pool-1-thread-7 ok pool-1-thread-8 ok pool-1-thread-2 ok pool-1-thread-5 ok pool-1-thread-10 ok pool-1-thread-9 ok pool-1-thread-6 ok
pool-1-thread-2 ok pool-1-thread-1 ok pool-1-thread-2 ok pool-1-thread-1 ok pool-1-thread-2 ok
Process finished with exit code 0
i <=6 第三个线程开始执行
1 2 3 4 5 6 7 8
pool-1-thread-1 ok pool-1-thread-3 ok pool-1-thread-2 ok pool-1-thread-3 ok pool-1-thread-1 ok pool-1-thread-2 ok
Process finished with exit code 0
i <=8 五个线程都在执行
1 2 3 4 5 6 7 8 9 10
pool-1-thread-1 ok pool-1-thread-5 ok pool-1-thread-2 ok pool-1-thread-3 ok pool-1-thread-4 ok pool-1-thread-2 ok pool-1-thread-5 ok pool-1-thread-1 ok
/** * @author Joker大雄 * @data 2022/8/23 - 11:39 **/ // 手动创建线程池 publicclassDemo02{ publicstaticvoidmain(String[] args){ /* 模拟银行业务: 默认开启2个窗口 最大有5个窗口 候客区有3个座位 */ ExecutorService threadExecutor = new ThreadPoolExecutor( 2, 5, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3), Executors.defaultThreadFactory(), // new ThreadPoolExecutor.AbortPolicy()); // 队列满了,不处理这任务,抛出异常 // new ThreadPoolExecutor.CallerRunsPolicy()); // 哪来的去哪 // new ThreadPoolExecutor.DiscardPolicy()); // 队列满了,丢掉任务,不会抛出异常 new ThreadPoolExecutor.DiscardOldestPolicy()); // 队列满了,尝试和最早的竞争,不会抛出异常 try { // 最大承载 队列+max值->3+8=8人 超过就会被拒绝策略接收,从而抛出异常 for (int i = 1; i <=9; i++) { threadExecutor.execute(()->{ System.out.println(Thread.currentThread().getName()+" ok"); }); } } catch (Exception e) { e.printStackTrace(); } finally { // 线程池使用完,程序结束,关闭线程池 threadExecutor.shutdown(); } } }
AbortPolicy() 队列满了,不处理这个任务,抛出异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
pool-1-thread-1 ok pool-1-thread-5 ok pool-1-thread-1 ok pool-1-thread-4 ok pool-1-thread-3 ok pool-1-thread-2 ok pool-1-thread-1 ok pool-1-thread-5 ok java.util.concurrent.RejectedExecutionException: Task com.jokerdig.pool.Demo02$$Lambda$1/1831932724@7699a589 rejected from java.util.concurrent.ThreadPoolExecutor@58372a00[Running, pool size = 5, active threads = 3, queued tasks = 0, completed tasks = 5] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) at com.jokerdig.pool.Demo02.main(Demo02.java:30)
Process finished with exit code 0
CallerRunsPolicy() 哪来的去哪
1 2 3 4 5 6 7 8 9 10 11
pool-1-thread-1 ok pool-1-thread-4 ok main ok pool-1-thread-3 ok pool-1-thread-2 ok pool-1-thread-3 ok pool-1-thread-4 ok pool-1-thread-1 ok pool-1-thread-5 ok
Process finished with exit code 0
DiscardPolicy() 队列满了,丢掉任务,不会抛出异常
1 2 3 4 5 6 7 8 9 10
pool-1-thread-2 ok pool-1-thread-4 ok pool-1-thread-3 ok pool-1-thread-3 ok pool-1-thread-4 ok pool-1-thread-1 ok pool-1-thread-2 ok pool-1-thread-5 ok
Process finished with exit code 0
DiscardOldestPolicy() 队列满了,尝试和最早的竞争,不会抛出异常
1 2 3 4 5 6 7 8 9 10
pool-1-thread-2 ok pool-1-thread-5 ok pool-1-thread-4 ok pool-1-thread-3 ok pool-1-thread-1 ok pool-1-thread-5 ok pool-1-thread-4 ok pool-1-thread-2 ok