Thread pool: Threadpoolexecutor

Source: Internet
Author: User

"The use and thinking of Threadpoolexecutor"  public threadpoolexecutor (int corepoolsize,                                        int  maximumpoolsize,                                        long keepAliveTime,                                        timeunit unit,                                        BlockingQueue<Runnable> workqueue,                                         threadfactory threadfactory,                                        rejectedexecutionhandler handler)     1. Parameter Interpretation
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, default value Threadpoolexecutor.abortpolicy ()

The unit optional parameters are several 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 () throws Java.util.concurrent.RejectedExecutionException exception
Threadpoolexecutor.callerrunspolicy () retries to add the current task, he will automatically repeatedly call the Execute () method
Threadpoolexecutor.discardoldestpolicy () Abandon the old task
Threadpoolexecutor.discardpolicy () Discard the current task


2. Method Invocation
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.


3. Processing mechanism
When a task is added to the thread pool by the Execute (Runnable) method:
If the number of threads in the thread pool is less than corepoolsize at this point, even if the threads in the thread pool are idle, a new thread is created to handle the task being added.
If the number in the thread pool is equal to 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.

Handle tasks for a long while:
Core thread corepoolsize, Task queue workqueue, maximum thread maximumpoolsize, if all three are full, use handler to handle the rejected task.
When processing a 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.

4. Some ways to create a thread pool:   public static ExecutorService  newFixedThreadPool ( int nThreads) {      return new ThreadPoolExecutor(nThreads,                                  nThreads,                                  0L,                                  TimeUnit.MILLISECONDS,                                  new LinkedBlockingQueue<Runnable>());     } public static ExecutorService  newCachedThreadPool () {      return new ThreadPoolExecutor( 0 ,                                  Integer.MAX_VALUE,                                  60L,                                  TimeUnit.SECONDS,                                  new SynchronousQueue<Runnable>());     } public static ExecutorService  newSingleThreadExecutor () {      return new FinalizableDelegatedExecutorService( new ThreadPoolExecutor( 1 ,                                                                          1 ,                                                                          0L,                                                                          TimeUnit.MILLISECONDS,                                                                          new LinkedBlockingQueue<Runnable>()));

 

Thread pool: Threadpoolexecutor

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.