/** * Creates a thread pool that maintains enough threads to support * the given parallelism level, and may use multiple queues to * reduce contention. The parallelism level corresponds to the * maximum number of threads actively engaged in, or available to * engage in, task processing. The actual number of threads may * grow and shrink dynamically. A work-stealing pool makes no * guarantees about the order in which submitted tasks are * executed. * * @param parallelism the targeted parallelism level * @return the newly created thread pool * @throws IllegalArgumentException if {@code parallelism <= 0} * @since 1.8 */ publicstatic ExecutorService newWorkStealingPool(int parallelism){ returnnew ForkJoinPool (parallelism, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }
/** * Creates a work-stealing thread pool using all * {@link Runtime#availableProcessors available processors} * as its target parallelism level. * @return the newly created thread pool * @see #newWorkStealingPool(int) * @since 1.8 */ publicstatic ExecutorService newWorkStealingPool(){ returnnew ForkJoinPool //Runtime.getRuntime().availableProcessors()返回的是可用处理器数目 (Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }
/** * 总是抛出异常 * Always throws RejectedExecutionException. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task * @throws RejectedExecutionException always */ publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e){ thrownew RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); } }
(2)CallerRunsPolicy 策略
只要线程池未关闭,该策略直接在调用者线程中,运行当前的被丢弃的任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * A handler for rejected tasks that runs the rejected task * directly in the calling thread of the {@code execute} method, * unless the executor has been shut down, in which case the task * is discarded. */ publicstaticclassCallerRunsPolicyimplementsRejectedExecutionHandler{ publicCallerRunsPolicy(){ }
/** * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e){ if (!e.isShutdown()) { r.run(); } } }
/** * A handler for rejected tasks that discards the oldest unhandled * request and then retries {@code execute}, unless the executor * is shut down, in which case the task is discarded. */ publicstaticclassDiscardOldestPolicyimplementsRejectedExecutionHandler{ publicDiscardOldestPolicy(){ }
/** * Obtains and ignores the next task that the executor * would otherwise execute, if one is immediately available, * and then retries execution of task r, unless the executor * is shut down, in which case task r is instead discarded. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e){ if (!e.isShutdown()) { //获取队列,并且将e插入到队列中,等待合适的时间去执行 e.getQueue().poll(); //执行任务 e.execute(r); } } }
(4)DiscardPolicy 策略
这个策略默默丢弃无法处理的任务,并且不做任何处理
1 2 3 4 5 6 7 8 9 10 11
/** * A handler for rejected tasks that silently discards the * rejected task. */ publicstaticclassDiscardPolicyimplementsRejectedExecutionHandler{ publicDiscardPolicy(){ } publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e){ //什么也不做 } }