Threadpoolexecutor Usage Introduction

Source: Internet
Author: User
Tags terminates

private static Executorservice exec = new Threadpoolexecutor (8, 8, 0L, timeunit.milliseconds, New Linkedblockingq Ueue<runnable> (100000),

New Threadpoolexecutor.callerrunspolicy ());

First, Introduction

The thread pool class is java.util.concurrent.ThreadPoolExecutor and is commonly constructed by:

Threadpoolexecutor (int corepoolsize, int maximumpoolsize,

Long KeepAliveTime, Timeunit unit,

Blockingqueue<runnable> WorkQueue,

Rejectedexecutionhandler handler)

Corepoolsize: Thread pool maintains a minimum number of threads

Maximumpoolsize: Thread pool maintains the maximum number of threads

KeepAliveTime: The thread pool maintains the idle time allowed by threads

Unit: The thread pool maintains the units of idle time allowed by threads

WorkQueue: Buffer queue used by the thread pool

Handler: thread pool processing policy for rejected tasks

A task is added to the thread pool by the Execute (Runnable) method, the task is an object of type Runnable, and the task is executed by the run () method of the Runnable type object.

When a task is added to the thread pool by the Execute (Runnable) method:

If the number in the thread pool is less than corepoolsize at this point, even if the threads in the thread pool are idle, create a new thread to handle the task being added.

If the number in the thread pool at this time equals corepoolsize, but the buffer queue Workqueue is not full, then the task is placed in the buffer queue.

If the number of threads in the thread pool is greater than corepoolsize, the buffer queue Workqueue full, and the number of thread pools is less than maximumpoolsize, a new thread is built to handle the task being added.

If the number of threads in the thread pool is greater than corepoolsize, the buffer queue is workqueue full, and the number in the thread pool equals maximumpoolsize, the task is handled by handler the policy specified. That is: the priority of the processing task is: Core thread corepoolsize, Task queue workqueue, maximum thread maximumpoolsize, if all three are full, use handler to handle the rejected task.

When the number of threads in the thread pool is greater than corepoolsize, if a thread is idle for more than KeepAliveTime, the thread is terminated. This allows the thread pool to dynamically adjust the number of threads in the pool.

The Unit optional parameter is a few static properties in Java.util.concurrent.TimeUnit:

nanoseconds,

microseconds,

MILLISECONDS,

SECONDS.

Workqueue commonly used are: Java.util.concurrent.ArrayBlockingQueue

Handler has four options:

Threadpoolexecutor.abortpolicy ()

Throw Java.util.concurrent.RejectedExecutionException exception

Threadpoolexecutor.callerrunspolicy ()

The rejectedexecution method is called when the Rejectedexecutionexception exception is thrown

(If the main thread is not closed, the main thread calls the Run method, the source code is as follows

public void Rejectedexecution (Runnable R, Threadpoolexecutor e) {if (!e.isshutdown ()) {R.run             (); }         }

)

Threadpoolexecutor.discardoldestpolicy ()

Abandon the old task

Threadpoolexecutor.discardpolicy ()

Abandon the current task

Second, relevant reference

A executorservice that uses a number of possible pool threads to perform each submitted task, typically using the Executors factory method configuration.

The thread pool resolves two different issues: because of the reduced overhead of each task invocation, they typically provide enhanced performance when performing a large number of asynchronous tasks, and can also provide methods for binding and managing resources, including the threads that are used to perform collection tasks. Each threadpoolexecutor also maintains some basic statistical data, such as the number of completed tasks.

For ease of use across a large number of contexts, this class provides a number of adjustable parameters and extension hooks. However, programmers are strongly advised to use the more convenient executors factory method Executors.newcachedthreadpool () (without a boundary pool, which can be used for automatic thread recycling), Executors.newfixedthreadpool ( int) (fixed-size thread pool) and Executors.newsinglethreadexecutor () (single background thread), which have predefined settings for most usage scenarios. Otherwise, when you manually configure and adjust this class, use the following guidelines:

Core and Maximum pool size

The threadpoolexecutor will automatically adjust the pool size based on the boundaries set by Corepoolsize (see Getcorepoolsize ()) and maximumpoolsize (see Getmaximumpoolsize ()). When a new task is committed in method execute (java.lang.Runnable), if the running thread is less than corepoolsize, a new thread is created to process the request, even if the other worker threads are idle. If you run more threads than corepoolsize and less than maximumpoolsize, a new thread is created only when the queue is full. If you set the same corepoolsize and Maximumpoolsize, a fixed-size thread pool is created. If you set Maximumpoolsize to a basic unbounded value (such as Integer.max_value), the pool is allowed to accommodate any number of concurrent tasks. In most cases, the core and maximum pool sizes are only set on a construction basis, but they can also be changed dynamically using setcorepoolsize (int) and setmaximumpoolsize (int).

On-Demand construction

By default, the core thread can be dynamically overridden with method Prestartcorethread () or prestartallcorethreads (), even if it was originally created and started only when a new task is needed.

Create a new thread

Create a new thread using Threadfactory. If not otherwise stated, threads are created in the same threadgroup using Executors.defaultthreadfactory (), and these threads have the same norm_priority priority and non-daemon status. By providing different threadfactory, you can change the name of the thread, the thread group, the priority, the daemon state, and so on. If threadfactory fails to create a thread when returning null from Newthread, the executor continues to run, but cannot perform any tasks.

Keep Active Time

If there are currently more than corepoolsize threads in the pool, these extra threads will terminate when the idle time exceeds KeepAliveTime (see Getkeepalivetime (Java.util.concurrent.TimeUnit)). This provides a way to reduce resource consumption when the pool is inactive. If the pool becomes more active later, you can create a new thread. You can also use Method Setkeepalivetime (long, java.util.concurrent.TimeUnit) to dynamically change this parameter. Use the value of Long.max_value timeunit.nanoseconds to effectively disable idle threads from the previous termination state before closing.

Queuing

All blockingqueue can be used to transfer and maintain submitted tasks. You can use this queue to interact with the pool size:

A. If you run fewer threads than Corepoolsize, Executor always prefers to add new threads without queuing.

B. If the thread running is equal to or more than corepoolsize, Executor always prefers to join the request to the queue without adding a new thread.

C. If the request cannot be queued, a new thread is created unless the thread is created beyond maximumpoolsize, in which case the task is rejected.

There are three common strategies for queuing:

Submit directly. The default option for the work queue is synchronousqueue, which will submit tasks directly to the thread without maintaining them. Here, if there is no thread available to run the task immediately, attempting to join the task to the queue will fail, and a new thread will be constructed. This policy avoids locking when processing a collection of requests that may have internal dependencies. Direct submissions typically require unbounded maximumpoolsizes to avoid rejecting newly submitted tasks. This policy allows the possibility of an increase in the number of lines that are allowed to continue when the command arrives in a row that exceeds the average that the queue can handle.

unbounded queues. Using unbounded queues (for example, linkedblockingqueue that do not have a predefined capacity) causes new tasks to be queued when all corepoolsize threads are busy. This way, the created thread will not exceed corepoolsize. (therefore, the value of the maximumpoolsize is not valid.) When each task is completely independent of other tasks, that is, when task execution does not affect each other, it is appropriate to use a unbounded queue, for example, in a Web page server. This queueing can be used to handle transient burst requests, which allow the possibility of an increase in the number of lines that are allowed to occur when the command reaches an average of more than the queue can handle.

bounded queues. When using limited maximumpoolsizes, bounded queues (such as arrayblockingqueue) help prevent resource exhaustion, but may be difficult to adjust and control. The queue size and maximum pool size may need to be compromised: using large queues and small pools minimizes CPU usage, operating system resources, and context switching overhead, but can result in artificially reduced throughput. If tasks are frequently blocked (for example, if they are I/O boundaries), the system may schedule more threads than you permit. Using small queues typically requires a large pool size, high CPU utilization, but may encounter unacceptable scheduling overhead, which also reduces throughput.

Rejected tasks

New tasks submitted in method execute (java.lang.Runnable) are rejected when Executor is closed and Executor uses a limited boundary for the maximum thread and work queue capacity and is already saturated. In both of these cases, the Execute method calls its Rejectedexecutionhandler rejectedexecutionhandler.rejectedexecution (java.lang.Runnable, Java.util.concurrent.ThreadPoolExecutor) method. The following four predefined handler policies are available:

A. In the default Threadpoolexecutor.abortpolicy, the handler is rejected and the runtime Rejectedexecutionexception is thrown.

B. In Threadpoolexecutor.callerrunspolicy, the thread invokes the execute itself that runs the task. This strategy provides a simple feedback control mechanism that can slow down the submission of new tasks.

C. In Threadpoolexecutor.discardpolicy, the tasks that cannot be performed are deleted.

D. In Threadpoolexecutor.discardoldestpolicy, if the execution program has not been closed, the task at the head of the work queue is deleted, and then the execution of the program is retried (repeat this process if it fails again).

It is also possible to define and use other kinds of rejectedexecutionhandler classes, but doing so requires great care, especially when the policy is used only for specific capacity or queueing policies.

Hook method

This class provides protected overridable BeforeExecute (Java.lang.Thread, java.lang.Runnable) and AfterExecute (Java.lang.Runnable, Java.lang.Throwable) method, which is called before and after each task is executed. They can be used to manipulate the execution environment, for example, reinitialize threadlocal, collect statistics, or add log entries. In addition, you can override method terminated () to perform all special processing that needs to be done after the Executor is completely terminated.

If the hook or callback method throws an exception, the internal worker thread fails sequentially and terminates abruptly.

Queue Maintenance

Method Getqueue () allows access to the work queue for monitoring and debugging purposes. Strongly oppose the use of this method for any other purpose. Both the Remove (java.lang.Runnable) and Purge () methods can be used to help with storage reclamation when a large number of queued tasks are canceled.

First, examples

To create the Testthreadpool class:

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) {    &NBSP;&NB sp;     //Construction of a thread pool           Threadpoolexecutor ThreadPool = new Threadpoolexecutor (2, 4, 3,                   Timeunit.seconds, New arrayblockingqueue<runnable> (3),                   New Threadpoolexecutor.discardoldestpolicy ());            for (int i = 1; I & Lt; = Producetaskmaxnumber; i++) {              try {                   String task = "[email protected]" + i;                   SYSTEM.OUT.PRINTLN ("Create task and submit to thread pool:" + Task);                   Threadpool.execute (New Threadpooltask (Task));                     Thread.Sleep (producetasksleeptime);              catch (Exception e) {                   e.printstacktrace ();              }  &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;  }     } }  view Plain Import java.util.concurrent.arrayblockingqueue;& nbsp Import java.util.concurrent.threadpoolexecutor;  Import java.util.concurrent.timeunit;    Public Class Testthreadpool {        private static int producetasksleeptime = 2;  &NBSP;&NBSP;&NB sp;       private static int producetaskmaxnumber = 10;        public s tatic void Main (string[] args) {           /construction of a thread pool     & nbsp;     threadpoolexecutor threadPool = new Threadpoolexecutor (2, 4, 3,                   Timeunit.seconds, New Arrayblockingqueue <Runnable> (3),                   New Threadpoolexecutor.discardoldestpolicy ());  &nbsp         for (int i = 1; I <= producetaskmaxnumber; i++) {     & nbsp;        try {                   String task = "[email protected]" + i;                   System.out.println ("Create task and commit to thread pool:" + Task);                   Threadpool.execute (New Threadpooltask (Task));                     Thread.Sleep (producetasksleeptime);               catch (Exception e) {                   e.printstacktrace ();  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;     }         }     }  } 

Create Threadpooltask class: View Plaincopy to Clipboardprint? Import java.io.serializable;    public class Threadpooltask implements Runnable, Serializable {    &N bsp;   Private Object attachdata;        threadpooltask (object tasks) {           this.attachdata = tasks;     }         public void Run () {                     System.out.println ("Start performing tasks:" + attachdata);                     attachdata = null;     }        public Object gettask () {          return this.attachdata;      } }  view Plain import java.io.serializable;    public class Threadpooltas K Implements RuNnable, Serializable {        private Object attachdata;        Threadpooltask (Object tasks) {          this.attachdata = tasks;      }        public void Run () {                     System.out.println ("Start task:" + attachdata);                     attachdata = null;      }        public Object gettask () {      & nbsp;   return this.attachdata;     } } 

Execution Result:

Create the task and submit it to the thread pool: [Email protected] 1

Start performing tasks: [email protected] 1

Create the task and submit it to the thread pool: [Email protected] 2

Start performing tasks: [Email protected] 2

Create the task and submit it to the thread pool: [Email protected] 3

Create the task and submit it to the thread pool: [Email protected] 4

Start performing tasks: [Email protected] 3

Create the task and submit it to the thread pool: [Email protected] 5

Start performing tasks: [Email protected] 4

Create the task and submit it to the thread pool: [Email protected] 6

Create the task and submit it to the thread pool: [Email protected] 7

Create the task and submit it to the thread pool: [Email protected] 8

Start performing tasks: [Email protected] 5

Start performing tasks: [Email protected] 6

Create the task and submit it to the thread pool: [Email protected] 9

Start performing tasks: [email protected] 7

Create the task and submit it to the thread pool: [Email protected] 10

Start performing tasks: [Email protected] 8

Start performing tasks: [Email protected] 9

Start performing tasks: [email protected] 10

threadpoolexecutor Configuration

First, Threadpoolexcutor for some executor to provide a basic implementation, these executor are executors in the factory Newcahcethreadpool, Returned by Newfixedthreadpool and Newscheduledthreadexecutor. Threadpoolexecutor is a flexible and robust pool implementation that allows a wide variety of user customizations.

Second, the creation and destruction of threads

1, the core pool size, the maximum pool size and survival time together to manage the creation and destruction of threads.

2. The size of the core pool is the size of the target; the implementation of the thread pool attempts to maintain the size of the pool, and even if there is no task execution, the pool size is equal to the size of the core pool, and the pool does not create more threads until the task queue is filled. If the size of the current pool exceeds the size of the core pool, the thread pool terminates it.

3. The maximum pool size is the upper limit of the number of threads that can be active concurrently.

4, if a thread has been idle for longer than the time of survival, it will become a candidate to be recycled.

5. The Newfixedthreadpool factory sets the size of the core pool and the maximum pool size for the requested pool, and the pool never times out

6, Newcachethreadpool factory set the maximum pool size to integer.max_value, the core pool size is set to 0, and the timeout is set to one minute. This creates an infinitely expanded thread pool that reduces the number of threads in the case of reduced demand.

Third, management

1. Threadpoolexecutor allows you to provide a blockingqueue to hold the task awaiting execution. There are 3 basic ways to queue tasks: Infinite queues, limited queues, and synchronous handover.

2, Newfixedthreadpool and Newsinglethreadexectuor are used by default is an infinite linkedblockingqueue. If all worker threads are busy, the task waits in the queue. If the task continues to arrive quickly, exceeding the speed at which they are executed, the queue is incremented indefinitely. A prudent strategy is to use a limited queue, such as Arrayblockingqueue or limited linkedblockingqueue and priorityblockingqueue.

3, for large or unlimited pools, you can use Synchronousqueue, completely bypass the queue, directly to the task from the producer to the worker thread

4. You can use Priorityblockingqueue to schedule tasks by priority

Threadpoolexecutor Usage Introduction

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.