When the executor has been disabled and the Executor uses the finite boundary for maximum thread and work queue capacity, and is saturated, execute (Java. lang. runnable. In both cases, the execute method calls the rejectedexecutionhandler's rejectedexecutionhandler. rejectedexecution (Java. Lang. runnable, java. util. Concurrent. threadpoolexecutor) method. The following provides four predefined handler policies:
A. in the default threadpoolexecutor. abortpolicy, a running rejectedexecutionexception will be thrown if the handler is rejected.
B. In threadpoolexecutor. callerrunspolicy, the thread calls the execute itself that runs the task. This policy provides a simple feedback control mechanism to speed down the submission of new tasks.
C. In threadpoolexecutor. discardpolicy, tasks that cannot be executed will be deleted.
D. In threadpoolexecutor. discardoldestpolicy, if the execution program is not closed, the task in the Job Queue header will be deleted, and then the execution program will be retried (if the execution fails again, the process will be repeated ).
Defining and using other types of rejectedexecutionhandler classes is also possible, but you need to be very careful when doing so, especially when the policy is only used for a specific capacity or queuing policy.
Hook method
This class provides the rewritable beforeexecute (Java. lang. thread, Java. lang. runnable) and afterexecute (Java. lang. runnable, Java. lang. throwable) method. These two methods are called before and after each task is executed. They can be used to manipulate the execution environment; for example, reinitializing threadlocal, collecting statistics, or adding log entries. In addition, you can override the terminated () method to execute all the special processing that needs to be completed after the executor is completely terminated.
If an exception is thrown by the hook or callback method, the Internal auxiliary thread will fail and terminate suddenly.
Queue Maintenance
The getqueue () method allows access to the work queue for monitoring and debugging purposes. It is strongly opposed to using this method for any other purpose. Remove (Java. Lang. runnable) and purge () methods can be used to help with storage collection when a large number of queued tasks are canceled.
I. Example
Create a testthreadpool class:
Create a threadpooltask class:
Execution result:
Create a task and submit it to the thread pool: Task @ 1
Start task execution: Task @ 1
Create a task and submit it to the thread pool: Task @ 2
Start task execution: Task @ 2
Create a task and submit it to the thread pool: Task @ 3
Create a task and submit it to the thread pool: Task @ 4
Start task execution: Task @ 3
Create a task and submit it to the thread pool: Task @ 5
Start task execution: Task @ 4
Create a task and submit it to the thread pool: Task @ 6
Create a task and submit it to the thread pool: Task @ 7
Create a task and submit it to the thread pool: Task @ 8
Start task execution: Task @ 5
Start task execution: Task @ 6
Create a task and submit it to the thread pool: Task @ 9
Start task execution: Task @ 7
Create a task and submit it to the thread pool: Task @ 10
Start task execution: Task @ 8
Start task execution: Task @ 9
Start task execution: Task @ 10
From: http://blog.csdn.net/yangdengfeng2003/archive/2009/04/01/4042252.aspx
View plaincopy to clipboardprint?
- Import java. Io. serializable;
- Public class threadpooltask implements runnable, serializable {
- Private object attachdata;
- Threadpooltask (Object tasks ){
- This. attachdata = tasks;
- }
- Public void run (){
- System. Out. println ("START task execution:" + attachdata );
- Attachdata = NULL;
- }
- Public object gettask (){
- Return this. attachdata;
- }
- }
Import Java. io. serializable; </P> <p> public class threadpooltask implements runnable, serializable {</P> <p> private object attachdata; </P> <p> threadpooltask (Object tasks) {<br/> This. attachdata = tasks; <br/>}</P> <p> Public void run () {</P> <p> system. out. println ("START task execution:" + attachdata); </P> <p> attachdata = NULL; <br/>}</P> <p> Public object gettask () {<br/> return this. attachdata; <br/>}< br/>}
View plaincopy to clipboardprint?
- Import java. util. Concurrent. arrayblockingqueue;
- Import java. util. Concurrent. threadpoolexecutor;
- Import java. util. Concurrent. timeunit;
- Public class testthreadpool {
- Private Static int producetasksleeptime = 2;
- Private Static int producetaskmaxnumber = 10;
- Public static void main (string [] ARGs ){
- // Construct a thread pool
- Threadpoolexecutor threadpool = new threadpoolexecutor (2, 4, 3,
- Timeunit. Seconds, new arrayblockingqueue <runnable> (3 ),
- New threadpoolexecutor. discardoldestpolicy ());
- For (INT I = 1; I <= producetaskmaxnumber; I ++ ){
- Try {
- String task = "task @" + I;
- System. Out. println ("create a task and submit it to the thread pool:" + task );
- Threadpool.exe cute (New threadpooltask (task ));
- Thread. Sleep (producetasksleeptime );
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- }
- }
Import Java. util. concurrent. arrayblockingqueue; <br/> Import Java. util. concurrent. threadpoolexecutor; <br/> Import Java. util. concurrent. timeunit; </P> <p> public class testthreadpool {</P> <p> Private Static int producetasksleeptime = 2; </P> <p> Private Static int producetaskmaxnumber = 10; </P> <p> Public static void main (string [] ARGs) {</P> <p> // construct a thread pool <br/> threadpoolexecutor threadpool = new threadpoolexecutor (2, 4, 3, <br/> timeunit. seconds, new arrayblockingqueue <runnable> (3), <br/> New threadpoolexecutor. discardoldestpolicy (); </P> <p> for (INT I = 1; I <= producetaskmaxnumber; I ++) {<br/> try {<br/> string task = "task @" + I; <br/> system. out. println ("create a task and submit it to the thread pool:" + task); <br/> threadpool.exe cute (New threadpooltask (task); </P> <p> thread. sleep (producetasksleeptime); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>}