Java Thread Pool Scenario

Source: Internet
Author: User

Java altogether contains four thread pools: Newcachedthreadpool, Newfixedthreadpool, Newsinglethreadexecutor, Newscheduledthreadpool.

Newcachedthreadpool: As the name implies is a cacheable thread pool, in addition to maintaining the initial size of the thread pool, when the number of tasks outside the thread pool size, the new thread, and when the thread completes the task will not be destroyed immediately, but will be retained for a period of time (default 60s), This greatly reduces the resource consumption of thread creation and destruction, when the disadvantage of this thread pool is that the maximum size of threads is too large, and if used for high concurrency and longer tasks, it is easy to eat up all the memory. So it is used to perform short-term asynchronous small tasks, or low-volume scenarios.

Newfixedthreadpool: is a thread pool based on the unbounded queue, the number of threads maintained is fixed, and if the threads are busy, the new task will be placed in the unbounded queue. Suitable for long-term tasks. If it is used for short-term tasks, the number of tasks < The number of thread pools, the performance will not be much different from newcachedthreadpool, but when exceeded, because it is short-term, so the task will be kept in the queue, and be taken out, the time interval is very short, When the number of tasks is too high, it can cause a lot of wasted resources.

Newsinglethreadexecutor: As the name implies is only a single thread pool, the specific scenario is not very clear, in terms of threading meaning, for the order (FIFO, LIFO, priority) to perform the task of the scene, as well as the thread confinement (variables can only be accessed by a particular thread).

Newschedulethreadpool: If all threads are busy, the new task enters the Delayedworkqueue queue, which is a queue structure sorted by time-out. The application scenario for this thread is easy to know, and the thread pool is used for tasks that need to be performed periodically.

Thread pool Task Execution Flow:

When the thread pool is smaller than corepoolsize, the new commit task creates a new thread to perform the task, even if there are idle threads in the thread pools at this time.
When the thread pool reaches corepoolsize, the new commit task is put into Workqueue, waiting for the task to be scheduled to execute in the thread pool
When Workqueue is full and maximumpoolsize>corepoolsize, the new commit task creates a new thread to perform the task
When the number of submitted tasks exceeds maximumpoolsize, the new commit task is handled by Rejectedexecutionhandler
When the thread pool exceeds the corepoolsize thread and the idle time reaches KeepAliveTime, the idle thread is closed
When Allowcorethreadtimeout (true) is set, corepoolsize thread idle time in the thread pool reaches KeepAliveTime will also close
Note:

In general, if the thread pool task queue takes the Linkedblockingqueue queue, then no task is rejected (because there is no limit to the queue size), in which case the Threadpoolexecutor will only create threads with the minimum number of threads. This means that the thread pool size is ignored.

If the thread pool task queue uses the Arrayblockingqueue queue, then Threadpoolexecutor will take a very responsible algorithm, such as assuming that the thread pool has a minimum number of threads of 4, The maximum number of arrayblockingqueue used for 8 is 10. As the task arrives and is placed in the queue, the thread pool runs up to 4 threads (that is, the minimum number of threads). Even if the queue is fully filled, that is, there are 10 waiting tasks, Threadpoolexecutor will only take advantage of 4 threads. If the queue is full and a new task comes in, a new thread is started, and the task is not rejected because the queue is full, instead a new thread is started. The new thread runs the first task in the queue, freeing up space for the new task.

The idea behind this algorithm is that the pool uses only the core threads (4) for most of the time, even if there is an appropriate amount of tasks waiting to run in the queue. The thread pool can then be used as a throttle valve. If the squeeze requests become very large, then the pool will try to run more threads to clean up, and the second throttle-the maximum number of threads will work.

Reference article: https://www.cnblogs.com/sachen/p/7401959.html

Java Thread Pool Scenario

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.