Java-threadpoolexecutor class

Source: Internet
Author: User

Threadpoolexecutor provides four methods of construction:

Threadpoolexecutor (int corepoolsize, int maximumpoolsize, long keepalivetime,  Timeunit unit, blockingqueue<runnable> workqueue) creates a new  threadpoolexecutor with the given initial parameters and default  Thread factory and rejected execution handler. Threadpoolexecutor (int corepoolsize, int maximumpoolsize, long keepalivetime,  timeunit unit, blockingqueue<runnable> workqueue, rejectedexecutionhandler  Handler) creates a new threadpoolexecutor with the given initial  Parameters and default thread factory. Threadpoolexecutor (int corepoolsize, int maximumpoolsize, long keepalivetime,  Timeunit unit, blockingqueue<runnable> workqueue, threadfactory threadfactory) creates a new&nbsp threadpoolexecutor with the given initial parameters and default  Rejected execution handler. Threadpoolexecutor (int corepoolsize, int maximumpoolsize, long keepalivetime,  Timeunit unit, blockingqueue<runnable> workqueue, threadfactory threadfactory,  rejectedexecutionhandler handler) creates a new threadpoolexecutor with the  given initial parameters.

Of course, it can also be built from the executors class, but requires a strong type and manual configuration of some properties.

For parameters in the constructor:

Corepoolsize: Normal run-time thread volume.

Maximumpoolsize: The maximum number of threads that are accommodated.

KeepAliveTime: The life time of a thread

Unit: units corresponding to KeepAliveTime for time

WorkQueue: Queue

Handler: processor when the number of submission threads is greater than maximumpoolsize

Threadfactory: Thread Creation Factory

Description

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. 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.

handler has four ways of handling:

static class threadpoolexecutor.abortpolicya handler for rejected tasks  that throws a rejectedexecutionexception.static class  threadpoolexecutor.callerrunspolicya handler for rejected tasks that runs  the rejected task directly in the calling thread of the  execute method, unless the executor has been shut down, in  which case the task is discarded.static class  Threadpoolexecutor.discardoldestpolicya handler for rejected tasks that discards  the oldest unhandled request and then retries execute, unless  the executor is shut down, in which case the task is  discarded.static class threadpoolexecutor.discardpolicya handleR for rejected tasks that silently discards the rejected task. 

AbortPolicy: Throws an exception to the caller

Callerrunspolicy: In the caller's thread execution,

Discardoldestpolicy: Discards the waiting queue for the longest task and joins the task to the queue

Discardpolicy: Abandon Task

Part of the method description:

Execute: Perform a task

Getcorepoolsize: Gets the maximum number of normal run-time threads

Getpoolsize: Gets the current number of thread pools

Getqueue: Get wait queue

Getactivecount: Gets the number of threads being executed (estimated value)



Java-threadpoolexecutor class

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.