In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the working principle of 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 how java thread pool works.
Thread pool creation
First, take a look at the construction method with the most complete parameters of ThreadPoolExecutor:
① corePoolSize: the number of core threads in the thread pool. To put it bluntly, even if there are no tasks in the thread pool, there will be corePoolSize threads waiting for tasks.
② maximumPoolSize: the maximum number of threads. No matter how many tasks you submit, the maximum number of worker threads in the thread pool is maximumPoolSize.
③ keepAliveTime: the lifetime of the thread. When the number of threads in the thread pool is greater than corePoolSize, if there is no task to execute after waiting for keepAliveTime, the thread exits.
⑤ unit: this is used to specify the unit of keepAliveTime, such as seconds: TimeUnit.SECONDS.
⑥ workQueue: a blocking queue into which submitted tasks will be placed.
⑦ threadFactory: thread factory, used to create threads, mainly to name threads, the default factory thread name: pool-1-thread-3.
⑧ handler: reject policy, which is called when the thread in the thread pool is exhausted and the queue is full.
These are the parameters used to create a thread pool, which is often asked by an interviewer during an interview.
II. Thread pool execution process
Here is a diagram to illustrate the execution flow of the thread pool.
When the task is submitted to the thread pool, it will first determine whether the current number of threads is less than corePoolSize, if so, create a thread to execute the submitted task, otherwise put the task into the workQueue queue, if the workQueue is full, determine whether the current number of threads is less than maximumPoolSize, if so, create a thread to execute the task, otherwise handler will be called to indicate that the thread pool refuses to receive the task.
Here take the source code of jdk1.8.0_111 as an example, take a look at the specific implementation.
1. First take a look at the executor method of thread pool
①: determine whether the current number of active threads is less than corePoolSize. If so, call addWorker to create a thread to execute the task.
②: if not less than corePoolSize, add the task to the workQueue queue.
③: if the workQueue fails, a thread is created to execute the task. If the thread creation fails (when the current number of threads is not less than maximumPoolSize), reject (internal call handler) will be called to refuse to accept the task.
2. Take a look at the method implementation of addWorker.
This code is when a non-core thread is created, that is, core equals false. Determine whether the current number of threads is greater than or equal to maximumPoolSize, and return false if it is greater than or equal to, that is, the failure to create a thread in ③ mentioned above.
The second half of the addWorker method:
① creates a Worker object and also instantiates a Thread object.
② start start this thread
3. Go to Worker to see its implementation
You can see that threadFactory is called when the Worker is created to create a thread. Starting a thread in the ② above triggers the run method of Worker to be called by the thread.
4. Next, let's look at the logic of the runWorker method.
When the thread calls runWoker, the while loop calls the getTask method to read the task from the workerQueue and then executes the task. This thread does not exit as long as the getTask method does not return null.
5. Finally, let's take a look at the getTask method implementation.
① Let's forget about allowCoreThreadTimeOut, the default value of this variable is false. Wc > corePoolSize determines whether the current number of threads is greater than corePoolSize.
② if the current number of threads is greater than corePoolSize, the poll method of workQueue is called to get the task, and the timeout is keepAliveTime. If the length of the keepAliveTime is exceeded, poll returns null, and the while mentioned above exits sequentially and the thread is finished.
If the current number of threads is less than corePoolSize, the take method of workQueue is called to block the current.
Thank you for your reading. the above is the content of "how java thread pools work". After the study of this article, I believe you have a deeper understanding of the working principle of java thread pools. 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.