ThreadPoolExecutor线程池的五种拒绝策略
解决方法:
1.AbortPolicy:拒绝添加任务,并抛出异常,为默认的策略。例如pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
2.DiscardPolicy:抛弃当前任务,会直接丢掉这个任务并且不会有任何异常。例如pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
3.DiscardOldestPolicy:抛弃最老的任务。例如:pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
4.CallerRunsPolicy:让线程池中的线程去执行这个任务,如果线程池中没空闲的线程,那么主线程会自己去执行该任务,不会等待线程池中的线程去执行。pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
5.自定义策略:可以根据你自己的业务代码来处理。例如:pool.setRejectedExecutionHandler((r, executor) -> { //业务代码 });
向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti