Java Thread pool rejection policy

Source: Internet
Author: User
Tags throw exception

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.