Java thread Pool Analysis second bullet

Source: Internet
Author: User
Tags cpu usage

Last shared the principle of the Java thread pool, the various thread pools from the memory and CPU use of the analysis, so that readers understand the use of the thread pool after the principle

And how to use the thread pool in the actual project and how to select the blocking queue


1. Thread pool does not allow the use of executors to create, but through the threadpoolexecutor way, such a way to write students more clear thread pool operation rules, to avoid the risk of exhaustion of resources. Description: (1) using the Newfixedthreadpool memory model


(2) Singlethreadexecutor memory model
Conclusion: These two thread pools will appear oom when there is a large number of tasks coming, and the basic reason is that these two thread pool memory is linkedblockingqueue, And the default linkedblockingqueue is 2 of the 32, is an infinite number, so there will be a large number of tasks piled up in memory, resulting in memory leaks, on the thread pool core threads, the maximum number of threads, blocking queue thread factory and rejection policy can see my share before the article In the adoption if the Linkedblockingqueue must be set size, or the linked thread pool into too fast, will overflow memory
2. Using Newcachedthreadpool memory


(1): Newcachedthreadpool using Synchronousqueue This blocking queue, special features are; Synchronousqueue is also a queue, but its special is that it has no container inside, a production line, When it produces a product (that is, put), if no one currently wants to consume the product (that is, no thread is currently executing take), the production line must block, waiting for a consumer thread to invoke the take operation, and the take operation will wake the production line. At the same time, the consumer thread gets the product of the production line (that is, data transfer), a process called a pairing process (which can also be take and put after the same principle). This is considered to be a model of a one-to-one message delivery between threads and threads. With this thread pool, if the task can be executed quickly, there won't be a lot of tasks pushing the pool inside, and hiding won't cause oom, but, yes, we use threads to sometimes take time-consuming actions, so when we use this thread pool, there's a lot of work coming up, Also unsafe, will cause CPU in busy state, then the mobile phone will be hot, heat, battery durability problem (2): Newscheduledthreadpool memory Model:

The use of Newscheduledthreadpool is particularly dangerous, in the presence of a large number of tasks, not only will cause oom, but also will cause CPU occupancy rate is particularly high, Newscheduledthreadpool adopted is Delayedworkqueue Queues, unbounded delay blocking queue Delayedworkqueue

When using a custom thread pool, be aware of two points:
To use Linkedblockingqueue, you must specify the blocking queue size, or use synchronousqueue, which does not require a specified size because he is not a container the following demo is an example:
Executorservice Fixedthreadpool = new Threadpoolexecutor (2, 3, Timeunit.seconds, New linkedblockingqueue<> ( ),                 new Threadfactory () {                      @Override                      public Thread newthread (@NonNull Runnable r) {                          thread t = new Thread (r);                          t.setname ("Custom Thread");                          return T;                      }                 } , new Rejectedexecutionhandler () {            @ Override             public void RejectedExecution ( Runnable R, Threadpoolexecutor executor) {
}         });
The screenshot below is run up after memory and CPU usage

Summary: We usually use the thread pool, according to the specific business, classification of other processing to define the thread pool, not a brain on the use of the original thread pool, in

If there are not many requests for a task, it may not be a problem, but the user is large, concurrency, there will be a variety of problems, so in the definition of the thread pool, each thread to define the prefix to facilitate follow-up problems, but also to define a number of resolution strategies.

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.