In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how the thread pool of Java works". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Java's thread pool works.
Summary of ☕️ prerequisites
I believe everyone has used thread pool, so this kind of ThreadPoolExecutor should be no stranger to us.
The reason why we need to use thread pool, thread pool is mainly used to solve the problem of thread life cycle overhead and insufficient resources.
We can effectively prevent resource shortages by reusing threads for multiple tasks and controlling the number of thread pools.
This chapter focuses on sharing and analyzing JDK8's ThreadPoolExecutor core class to see how thread pools work.
Core principle of ☕️
The thread pool actually builds a producer-consumer model internally, which decouples task submission and task execution, and is not directly related to each other, so that tasks are well buffered and threads are reused.
The operation of thread pool is mainly divided into two parts: task management and thread management.
☕️ task management
Task management acts as a producer.
When a task is submitted, the thread pool determines the subsequent flow of the task:
Directly apply for the thread to perform the task
Buffered to queue for thread execution
Reject the task.
☕️ thread management
Thread management is the role of consumers
They are maintained uniformly in the thread pool, and threads are allocated according to task requests.
When the thread finishes executing the task, it will continue to get new tasks to execute.
Eventually, when the thread cannot get the task, the thread will be reclaimed.
Next, we will explain the thread pool running mechanism in detail in the following three parts:
How the thread pool maintains its own state.
How the thread pool manages tasks.
How the thread pool manages threads.
☕️ constructor ☕️ four constructors / / constructor-public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {this (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory () DefaultHandler) } / / Constructor II public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) {this (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue) ThreadFactory, defaultHandler) } / / Constructor three public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) {this (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue) Executors.defaultThreadFactory (), handler) } / / Constructor four public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory RejectedExecutionHandler handler) {if (corePoolSize < 0 | | maximumPoolSize
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.