publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler){}
SynchronousQueue<Runnable> queue = new SynchronousQueue<>(); RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
11:07:26:130 INFO [main] poolSize=1 11:07:26:130 INFO [pool-2-thread-1] run 11:07:26:132 INFO [main] poolSize=2 11:07:26:132 INFO [main] poolSize=3 11:07:26:132 INFO [pool-2-thread-2] run 11:07:26:133 INFO [main] poolSize=4 11:07:26:133 ERROR [main] RejectedExecutionException 11:07:26:133 INFO [main] poolSize=4 11:07:26:134 ERROR [main] RejectedExecutionException 11:07:26:134 INFO [main] poolSize=4 11:07:26:135 INFO [pool-2-thread-3] run 11:07:26:135 INFO [pool-2-thread-4] run 11:07:27:143 INFO [pool-2-thread-3] exit 11:07:27:143 INFO [pool-2-thread-2] exit 11:07:27:143 INFO [pool-2-thread-4] exit 11:07:27:143 INFO [pool-2-thread-1] exit 11:07:31:150 INFO [main] poolSize=2 11:07:31:150 INFO [main] exit
SynchronousQueue<Runnable> queue = new SynchronousQueue<>(); RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
11:12:20:113 INFO [pool-2-thread-1] run 11:12:20:113 INFO [main] poolSize=1 11:12:20:113 INFO [main] poolSize=2 11:12:20:113 INFO [pool-2-thread-2] run 11:12:20:113 INFO [main] poolSize=3 11:12:20:113 INFO [main] poolSize=4 11:12:20:113 INFO [pool-2-thread-3] run 11:12:20:113 INFO [main] poolSize=4 11:12:20:113 INFO [main] poolSize=4 11:12:20:113 INFO [pool-2-thread-4] run 11:12:21:128 INFO [pool-2-thread-4] exit 11:12:21:128 INFO [pool-2-thread-2] exit 11:12:21:128 INFO [pool-2-thread-3] exit 11:12:21:128 INFO [pool-2-thread-1] exit 11:12:25:119 INFO [main] poolSize=2 11:12:25:119 INFO [main] exit
对比上面的日志可以发现
同样也是只执行了4个任务
区别在于没有抛出异常,也就是说Discard策略直接拒绝,来异常都不给,没啥用
同步移交+CallerRun
SynchronousQueue<Runnable> queue = new SynchronousQueue<>(); RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
11:18:11:432 INFO [pool-2-thread-1] run 11:18:11:432 INFO [main] poolSize=1 11:18:11:434 INFO [main] poolSize=2 11:18:11:434 INFO [pool-2-thread-2] run 11:18:11:434 INFO [main] poolSize=3 11:18:11:435 INFO [main] poolSize=4 11:18:11:435 INFO [main] run 11:18:11:435 INFO [pool-2-thread-3] run 11:18:11:435 INFO [pool-2-thread-4] run 11:18:12:447 INFO [pool-2-thread-4] exit 11:18:12:447 INFO [main] exit 11:18:12:447 INFO [pool-2-thread-3] exit 11:18:12:447 INFO [pool-2-thread-2] exit 11:18:12:447 INFO [pool-2-thread-1] exit 11:18:12:447 INFO [main] poolSize=4 11:18:12:447 INFO [main] run 11:18:13:448 INFO [main] exit 11:18:13:448 INFO [main] poolSize=4 11:18:18:454 INFO [main] poolSize=2 11:18:18:454 INFO [main] exit