Java Multithreading review-thread pool---learning Notes (iii)

Source: Internet
Author: User

The thread pool produces the reason:
Create two problems caused by many threads:
1. Building a new thread involves interacting with the operating system, consumes some system resources, and when the newly created threads are used, the threads are destroyed, and then the system resources are consumed again when we create them, so if you create many short-life threads, you will consume a lot of system resources. Even bring a lot of pressure to the system.
2. Now regardless of the pressure to create a lot of threads, and simply from the creation of a large number of threads concurrently executing the task, the number of threads that our system can tolerate is limited, and creating a large number of threads can greatly degrade performance or even virtual machine crashes.
So here to solve these two problems caused by creating multiple threads is the meaning of the thread pool's birth.
the role of the thread pool:
1. Reduce the overhead of duplicate creation threads, the threads in the thread pool will not be destroyed after they are used, and the threads in the thread pool will continue to be used when there are new tasks (except for thread pools (newsinglethreadexecutor), which have only one threads, which is the effect of other thread pooling)
2. Control the number of concurrent threads, the thread pool can fix how many threads are in the thread pools, and will not create a large number of threads causing the virtual machine to crash (using thread pool with threads fixed (newfixedthreadpool))
types of thread pools:
(1) Newcachedthreadpool: (thread pool with cache)
Create a new thread if necessary; idle thread is retained 60s
(2) Newfixedthreadpool: (thread pool with fixed number of threads)
The number of threads in the thread pool is determined by the parameters
(3) Newsinglethreadexecutor: (thread pool for individual threads)
It executes each task sequentially in a single thread
(4) Newscheduledthreadpool: (a thread pool that can control delay time or cycle execution threads)
description of the specific four thread pools:
Newcachedthreadpool: This method constructs a thread pool, and for each task, if an idle thread is available, let it perform the task immediately, and if there are no idle threads available, create a new thread.
Newfixedthreadpool: This method constructs a thread pool with a fixed size, and if the number of committed tasks is more than the number of idle threads, the task that is not serviced is placed in the queue. When other tasks are complete, run them.
Newsinglethreadexecutor: is a degraded thread pool of size 1: There is one thread that executes the submitted task, one after the other.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Multithreading review-thread pool---learning Notes (iii)

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.