First, the constructor signature is as follows:
Public threadpoolexecutor (int corepoolsize,int maximumpoolsize,long keepalivetime,timeunit unit, Blockingqueue<runnable> Workqueue,rejectedexecutionhandler handler);
Parameter description:
corepoolsize Core threads, which refers to the maximum number of threads in a thread pool that are working at a maximumpoolsize value (no more than corepoolsize). &NBSP
maximumpoolsize refers to the maximum size of the thread pool (the maximum number of corepoolsize threads in the threading pools can be run). &NBSP
keepalivetime refers to the time-out period when the idle thread ends (the thread will stop for a long time when a thread is not working). &NBSP
unit is an enumeration that represents KeepAliveTime units (with nanoseconds, microseconds, MILLISECONDS, SECONDS, MINUTES, HOURS, days,7 selectable values).
workqueue represents the queue that holds the task (the queue of threads that need to be executed by the thread pool).
handler deny policy (how to handle the task after adding a task fails).
1, line Chengchigang when created, there is not a thread. The task queue is passed in as a parameter. However, even if there are tasks in the queue, the thread pool does not execute them immediately.
2. When the Execute () method is called to add a task, the thread pool makes the following judgment:
A. If the number of threads running is less than corepoolsize, then create the thread to run the task immediately;
B. If the number of threads running is greater than or equal to corepoolsize, then put this task into the queue.
C. If the queue is full and the number of threads running is less than maximumpoolsize, then create a thread to run the task;
D. If the queue is full and the number of threads running is greater than or equal to maximumpoolsize, then the thread pool throws an exception telling the caller that I can no longer accept the task.
3. When a thread finishes a task, it takes the next task from the queue to execute.
4, when a thread has nothing to do, more than a certain amount of time (KeepAliveTime), the thread pool will determine if the number of threads currently running is greater than corepoolsize, then the threads will be stopped. So when all the thread pool tasks are completed, it eventually shrinks to the size of the corepoolsize.
This process shows that you do not have to join the task first. Assuming that the queue size is 4,corepoolsize to 2,maximumpoolsize of 6, then when you join 15 tasks, the order of execution is similar: Perform task 1, 2 first, and then the task 3~6 into the queue. When the queue is full, tasks 7, 8, 9, 10 will be executed immediately, and task 11~15 will throw an exception. The final order is: 1, 2, 7, 8, 9, 10, 3, 4, 5, 6. Of course this process is for arrayblockingqueue<runnable> of the specified size, if it is linkedblockingqueue<runnable>, because there is no size limit for the queue, there is no such problem.
handler deny policy (how to handle the task after adding a task fails).
If you change to: New Threadpoolexecutor.discardpolicy (), then you will not throw an exception, just discard the task. If you don't write, you'll throw an exception.
Packagethread;ImportJava.util.concurrent.ArrayBlockingQueue;ImportJava.util.concurrent.ThreadPoolExecutor;ImportJava.util.concurrent.TimeUnit; Public classThreadpooltestImplementsRunnable { Public voidrun () {synchronized( This) { Try{System.out.println ("Thread Name:" +Thread.CurrentThread (). GetName ()); Thread.Sleep (3000);//hibernation is done to make the thread constructor released from the line after it has not finished executing}Catch(interruptedexception e) {e.printstacktrace (); } } } Public Static voidMain (string[] args)throwsinterruptedexception {arrayblockingqueue<Runnable> queue =NewArrayblockingqueue<runnable> (4);//thread queue fixed to 4Threadpoolexecutor executor =NewThreadpoolexecutor (2, 6, 1, timeunit.days, queue,NewThreadpoolexecutor.discardpolicy ()); for(inti = 0; I < 11; i++) {Executor.execute (NewThread (NewThreadpooltest (), "Testthread". Concat ("" +i))); intThreadsize =queue.size (); System.out.println ("Thread Queue Size--" +threadsize); } executor.shutdown (); }}
Turn-http://blog.csdn.net/shixing_11/article/details/7109471
Java thread pool threadpoolexecutor-go