It is discussed today because it is used in the nettyserver of Motan. This thread pool can be used as a thread pooling for business, and it customizes one of its own threading pools. Of course, it is based on the construction method and the Execute method in the Threadexecutor in the JDK, and then wraps the layer outside.
PublicThreadpoolexecutor (intCorepoolsize,intMaximumpoolsize,LongKeepAliveTime, timeunit unit, Blockingqueue<Runnable>WorkQueue, Threadfactory threadfactory, Rejectedexecutionhandler ha Ndler) {if(Corepoolsize < 0 | |maximumpoolsize<= 0 | |maximumpoolsize< Corepoolsize | |KeepAliveTime< 0) Throw Newillegalargumentexception (); if(WorkQueue = =NULL|| Threadfactory = =NULL|| Handler = =NULL) Throw NewNullPointerException (); This. corepoolsize =corepoolsize; This. maximumpoolsize =maximumpoolsize; This. WorkQueue =WorkQueue; This. KeepAliveTime =Unit.tonanos (KeepAliveTime); This. Threadfactory =threadfactory; This. Handler =handler;}
1 Public voidExecute (Runnable command) {2 if(Command = =NULL)3 Throw Newnullpointerexception ();4 /*5 * Proceed in 3 steps:6 *7 * 1. If fewer than corepoolsize threads is running, try to8 * Start a new thread with the given command as its first9 * task. The call to Addworker atomically checks runstate andTen * Workercount, and so prevents false alarms that would add One * Threads when it shouldn ' t, by returning false. A * - * 2. If a task can be successfully queued and then we still need - * To double-check whether we should has added a thread the * (because existing ones died since last checking) or that - * The pool shut down since entry to this method. So we - * Recheck State and if necessary rolled back the enqueuing if - * stopped, or start a new thread if there is none. + * - * 3. If We cannot queue task, then we try to add a new + * thread. If it fails, we know we are shut down or saturated A * And so reject the task. at */ - intc =ctl.get (); - if(Workercountof (c) <corepoolsize) { - if(Addworker (Command,true)) - return; -c =ctl.get (); in } - if(IsRunning (c) &&workqueue.offer (command)) { to intRecheck =ctl.get (); + if(! isrunning (Recheck) &&Remove (command)) - reject (command); the Else if(Workercountof (recheck) = = 0) *Addworker (NULL,false); $ }Panax Notoginseng Else if(!addworker (Command,false)) - reject (command); the}
The Execute method in Threadexecutor
It is clear from the comments of the Execute method that the traditional thread joins the thread pool execution process in 3 steps
Less than or equal to Coresize: Creates a thread's line
Greater than Coresize join queue
The queue is full and less than maxsize has idle threads to execute with idle threads, and no words are created for thread execution
Greater than maxsize deny policy execution
Now it needs to be modified in the following way
1 /**2 * <pre>3 * 4 * The code and ideas come mainly from:5 * 6 * Tomcat:7 * Org.apache.catalina.core.StandardThreadExecutor8 * 9 * Java.util.concurrentTen * Threadpoolexecutor Execute Execution Strategy: preferential offer to queue,queue after the full and then expand the thread to Maxthread, if it is Maxthread reject One * More suitable for CPU-intensive applications (such as runnable internal operations within the JVM, memory copy, or compute, etc.) A * - * Standardthreadexecutor Execute execution strategy: priority to expand the thread to Maxthread, then offer to queue, if full of reject - * More suitable for scenarios where the business process requires remote resources the * - * </pre>
Concrete way to reinvent a queue and work on the team in the way.
Thread pool Standardthreadexecutor in Tomcat