In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the knowledge points related to java thread pool". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what are the knowledge points related to java thread pool"?
The meaning of thread pool:
Thread is a scarce resource, and its creation and destruction is a heavy and expensive operation. While the java thread depends on the kernel thread, the creation thread needs to switch the operating system state. In order to avoid excessive resource consumption, it is necessary to try to reuse the thread to perform multiple tasks. Thread pool is a thread cache, which is responsible for unified allocation, tuning and monitoring of threads.
Advantages of thread pools:
Reuse existing threads, reduce thread creation and extinction overhead, and improve performance
Improve response speed so that when a task arrives, it can be executed immediately without waiting for thread creation
To improve the manageability of threads, threads are scarce resources. if created without restriction, it will not only consume system resources, but also reduce the stability of the system. Thread pool can be used for unified allocation, tuning and monitoring.
There are five ways to create a thread pool
FixedThreadPool and SingleThreadExecutor = > allowed request queue length is Integer.MAX_VALUE, which may pile up a large number of requests, resulting in OOM exception CachedThreadPool = > the number of threads allowed to be created is Integer.MAX_VALUE, which may create a large number of threads, resulting in OOM exception (memory overflow exception)
CorePoolSize: number of core threads maximumPoolSize: maximum number of threads keepAliveTime: survival time unit: unit of survival time workQueue: similar to when the bank is very busy, in addition to the core window, there are some temporary windows 3.4 interfaces are for temporary windows: handler: reject strategy when all the work is full, there will be a rejection strategy jdk comes with four rejection strategies You can also write your own rejection strategy 1: abandon the queue and then throw an exception 2: abandon the queue without throwing an exception 3: abandon the first few tasks in the queue and repeat task 4: the caller fallback mechanism allows the caller to do it on his own
Line city execution process: at the beginning, only two core threads are working, and then the next thread is queued in the work queue. If the work queue is full, a temporary thread is created, if the temporary thread is also satisfied.
If not, implement the rejection strategy.
Five states of the thread pool:
Running: ability to accept new tasks and handle tasks that have been added
Shutdown: do not accept new tasks, but can handle tasks that have been added
Stop: do not accept new tasks, do not process tasks that have been added, and interrupt tasks that are in progress
Tidying: all tasks have been terminated and the number of tasks in ctl is 0 (ctl is responsible for recording the running status of the thread pool and the number of active threads)
Terminated: if the thread pool terminates completely, the thread pool transitions to the terminated state
What are the work queues in the thread pool?
ArrayBlockingQueue: at the bottom are arrays, bounded queues, which is a very good choice if we are going to use the producer-consumer model.
LinkedBlockingQueue: the underlying list is a linked list, which can be used as an unbounded and bounded queue, so don't think of it as an unbounded queue.
SynchronousQueue: there is no space to store any elements, so you can choose between fair mode and unfair mode.
PriorityBlockingQueue: unbounded queue, based on the array, the data structure is a binary heap, the first and tree root node of the array is always the minimum.
For example, the principle of ArrayBlockingQueue to achieve concurrent synchronization: the principle is that both read and write operations need to acquire AQS exclusive locks before they can operate. If the queue is empty, the thread of the read operation enters the queue of the reader thread, waits for the writer thread to write a new element, and then wakes up the first waiting thread of the reader thread queue. If the queue is full, the writer thread enters the write thread queue, waits for the reader thread to remove the queue element to make room, and then wakes up the first waiting thread in the writer queue.
How to determine the number of threads in the thread pool
In general, if it is a CPU-intensive application, the thread pool size is set to Number1.
In general, for IO-intensive applications, the thread pool size is set to 2N+1.
In IO optimization, the higher the proportion of thread wait time, the more threads are needed, and the higher the proportion of thread CPU time is, the fewer threads are needed. Such an estimation formula may be more suitable: optimal number of threads = ((thread wait time + thread CPU time) / thread CPU time) * number of CPU
How to implement a thread pool with priority
Using the priority parameter, inheriting ThreadPoolExecutor uses the PriorityBlockingQueue priority queue.
Thank you for your reading. the above is the content of "what are the knowledge points related to java thread pool". After the study of this article, I believe you have a deeper understanding of what are the knowledge points related to java thread pool. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.