Use of Thread Pool (Java. util. Concurrent. threadpoolexecutor) (2)

Source: Internet
Author: User

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?
  1. Import java. Io. serializable;
  2. Public class threadpooltask implements runnable, serializable {
  3. Private object attachdata;
  4. Threadpooltask (Object tasks ){
  5. This. attachdata = tasks;
  6. }
  7. Public void run (){
  8. System. Out. println ("START task execution:" + attachdata );
  9. Attachdata = NULL;
  10. }
  11. Public object gettask (){
  12. Return this. attachdata;
  13. }
  14. }

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?
  1. Import java. util. Concurrent. arrayblockingqueue;
  2. Import java. util. Concurrent. threadpoolexecutor;
  3. Import java. util. Concurrent. timeunit;
  4. Public class testthreadpool {
  5. Private Static int producetasksleeptime = 2;
  6. Private Static int producetaskmaxnumber = 10;
  7. Public static void main (string [] ARGs ){
  8. // Construct a thread pool
  9. Threadpoolexecutor threadpool = new threadpoolexecutor (2, 4, 3,
  10. Timeunit. Seconds, new arrayblockingqueue <runnable> (3 ),
  11. New threadpoolexecutor. discardoldestpolicy ());
  12. For (INT I = 1; I <= producetaskmaxnumber; I ++ ){
  13. Try {
  14. String task = "task @" + I;
  15. System. Out. println ("create a task and submit it to the thread pool:" + task );
  16. Threadpool.exe cute (New threadpooltask (task ));
  17. Thread. Sleep (producetasksleeptime );
  18. } Catch (exception e ){
  19. E. printstacktrace ();
  20. }
  21. }
  22. }
  23. }

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/>}

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.