Java multithreading: threadpoolexecutor

Source: Internet
Author: User

Public threadpoolexecutor (INT corepoolsize,
Int maximumpoolsize,
Long KeepAliveTime,
Timeunit unit,
Blockingqueue <runnable> workqueue,
Threadfactory,
Rejectedexecutionhandler handler)
1. Parameter explanation
Corepoolsize: Minimum number of threads maintained by the thread pool
Maximumpoolsize: Maximum number of threads maintained by the thread pool
KeepAliveTime: the idle time allowed by the thread pool to maintain the thread
Unit: the unit of idle time allowed by the thread pool maintenance thread.
Workqueue: The Buffer Queue used by the thread pool.
Handler: The processing policy of the thread pool to reject tasks. The default value is threadpoolexecutor. abortpolicy ()

The optional parameters of unit are several static attributes in Java. util. Concurrent. timeunit: nanoseconds, microseconds, milliseconds, and seconds.
Workqueue is commonly used: Java. util. Concurrent. arrayblockingqueue
Handler has four options:
Threadpoolexecutor. abortpolicy () throws java. util. Concurrent. rejectedexecutionexception
Threadpoolexecutor. callerrunspolicy () retries to add the current task. It will automatically repeat the execute () method
Threadpoolexecutor. discardoldestpolicy () discard the old task
Threadpoolexecutor. discardpolicy () discards the current task

2. method call
A task is added to the thread pool using the execute (runnable) method. A task is a runnable object. The execution method of a task is the run () method of a runnable object.

3. Processing Mechanism
When a task is added to the thread pool through the execute (runnable) method:
If the number of threads in the thread pool is smaller than corepoolsize, a new thread should be created to process the added tasks even if all threads in the thread pool are idle.
If the number in the thread pool is equal to corepoolsize, but the Buffer Queue workqueue is not full, the task is put into the buffer queue.
If the number in the thread pool is greater than corepoolsize, the Buffer Queue workqueue is full, and the number in the thread pool is smaller than maximumpoolsize, a new thread is created to process the added task.
If the number in the thread pool is greater than corepoolsize, the Buffer Queue workqueue is full, and the number in the thread pool is equal to maximumpoolsize, the handler policy is used to process the task.

When many tasks are processed:
Core Thread corepoolsize, task queue workqueue, and maximumpoolsize. If all three are full, handler is used to process the rejected task.
When there are few tasks to process:
When the number of threads in the thread pool is greater than corepoolsize, if the idle time of a thread exceeds KeepAliveTime, the thread will be terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.

4. Some methods for creating 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> ()));
}

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.