Class diagram for thread pool rejection policy: roughly seven or so
Look at the source code of each class:
AbortPolicy (direct throw exception)
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()); } }
Abortpolicywithreport (logging)
public class Abortpolicywithreport extends Threadpoolexecutor.abortpolicy {static private final Logger Logger = Logger Init.logger; Private final String ThreadName; Private final URL url; Public Abortpolicywithreport (String threadname, url url) {this.threadname = ThreadName; This.url = URL; } @Override public void Rejectedexecution (Runnable R, Threadpoolexecutor e) {String msg = String.Format ("Thr EAD Pool is exhausted! "+" Thread Name:%s, pool Size:%d (active:%d, Core:%d, Max:%d, largest:%d), Ta SK:%d (completed:%d), "+" Executor Status: (isshutdown:%s, isterminated:%s, isterminating:%s), in%s://%s :%d! ", ThreadName, E.getpoolsize (), E.getactivecount (), E.getcorepoolsize (), E.getmaximumpoolsize (), E.get Largestpoolsize (), E.gettaskcount (), E.getcompletedtaskcount (), E.isshutdown (), e.isterminated (), E.isTermi Nating (), Url.getprotocol (), Url.getip (), Url.getporT ()); Logger.warn (msg); throw new Rejectedexecutionexception (msg); }}
Callerrunspolicy (caller execution)
public static class CallerRunsPolicy implements RejectedExecutionHandler { public CallerRunsPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
Discardoldestpolicy (discards the oldest unhandled task in the thread pool)
public static class DiscardOldestPolicy implements RejectedExecutionHandler { public DiscardOldestPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
Discardpolicy (direct refusal, no processing)
public static class DiscardPolicy implements RejectedExecutionHandler { public DiscardPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { } }
Ignorerunspolicy (Ignore run test, dump JVM information)
private static class IgnoreRunsPolicy implements RejectedExecutionHandler { private final static Logger LOGGER = LoggerInit.LOGGER; public volatile boolean hasDump = false; public IgnoreRunsPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { dumpJVMInfo(); ?//方法太长不予展示,就是这个dump jvm信息的意思 // ignore it when thread full and it will be timeout response for client throw new RejectedExecutionException(); }
Newthreadrunspolicy (Create a new temporary thread inside the memoryawarethreadpoolexecutor to handle)
private static final class NewThreadRunsPolicy implements RejectedExecutionHandler { public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { final Thread t = new Thread(r, "Temporary task executor"); t.start(); } catch (Throwable e) { throw new RejectedExecutionException( "Failed to start a new thread", e); } } }
Reading the source is not the thread pool rejected the policy has a deeper understanding?
Java Thread pool rejection policy