In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the types, differences and usage scenarios of thread pools, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
A thread goes through the following process from submit to execution:
The thread pool determines whether all the threads in the core thread pool are executing the task, and if not, create a new worker thread to execute the task. If all the threads in the core thread pool are performing tasks, proceed to the next process
The thread pool determines whether the work queue is full. If the work queue is not full, the newly submitted tasks are stored in this work queue. If the work queue is full, proceed to the next process.
The thread pool determines whether all its internal threads are working. If not, a new worker thread is created to execute the task. If it is full, leave it to the saturation strategy to handle the task.
Mission rejection strategy?
There are four built-in implementation policies and a user-defined reject policy.
AbortPolicy is the default blocking policy for the java thread pool, which does not perform this task and directly throws a runtime exception. Keep in mind that ThreadPoolExecutor.execute requires try catch, otherwise the program will exit directly.
DiscardPolicy discards directly, the task is not executed, and the empty method.
DiscardOldestPolicy abandons one of head's tasks from the queue and execute the task again.
CallerRunsPolicy executes this command in the thread that calls execute, blocking the entry.
The user defines the deny policy to implement RejectedExecutionHandler, and defines the policy pattern.
Again, note that the ThreadPoolExecutor.submit () function, the execute method called internally by this method, returns the result of execute execution, but if the task is not executed (rejected), the future.get () returned by submit will wait.
Future is actually a runnable inside, and encapsulates the command. When the command is finished, the future will return a value.
NewCachedThreadPool:
Bottom layer: return ThreadPoolExecutor instance, corePoolSize is 0bot, maximumPoolSize, Integer.MAX_VALUE;keepAliveTime is 60L, unit is TimeUnit.SECONDS;workQueue, SynchronousQueue (synchronization queue)
Popular: when a new task arrives, it will be inserted into SynchronousQueue. Because SynchronousQueue is a synchronous queue, it will look for available threads in the pool to execute. If there is a thread available, it will be executed. If no thread is available, a thread will be created to execute the task. If the idle time of the thread in the pool exceeds the specified size, the thread will be destroyed.
Applicable: execute many short-term asynchronous Mini Program or light-loaded servers
NewFixedThreadPool:
Bottom layer: return ThreadPoolExecutor instance. Receive parameter: number of threads set: nThread,corePoolSize: nThread,maximumPoolSize: nThread;keepAliveTime: 0L (unlimited); unit: TimeUnit.MILLISECONDS;WorkQueue: new LinkedBlockingQueue () No unblocking queue
Popular: create a pool that can hold a fixed number of threads, the survival time of each thread is unlimited, when the pool is full, no more threads are added; if all threads in the pool are busy, for new tasks will enter the blocking queue (unbounded blocking queue)
Applicable: perform long-term tasks with much better performance
NewSingleThreadExecutor:
Bottom layer: ThreadPoolExecutor instance packaged by FinalizableDelegatedExecutorService, corePoolSize is 1 position, maximumPoolSize is 1, keepAliveTime is 0 L, unit is: TimeUnit.MILLISECONDS;workQueue is: new LinkedBlockingQueue () No unblocking queue
Popular: create a thread pool with only one thread, and the thread's survival time is unlimited; when the thread is busy, it will enter the blocking queue for new tasks (unbounded blocking queue)
Applicable: a scenario in which a task is executed
NewScheduledThreadPool:
Bottom layer: create a ScheduledThreadPoolExecutor instance. CorePoolSize is the passed parameter, maximumPoolSize is Integer.MAX_VALUE;keepAliveTime, and the unit is 0: TimeUnit.NANOSECONDS;workQueue: new DelayedWorkQueue () A queue sorted in ascending order of timeout
Popular: create a fixed-size thread pool with unlimited thread survival time. The thread pool can support scheduled and periodic task execution. If all threads are busy, new tasks will enter the DelayedWorkQueue queue, which is a queue structure sorted by timeout.
Applicable: scenarios where tasks are executed periodically
Thread pool task execution process:
When the thread pool is less than corePoolSize, the newly committed task creates a new thread to execute the task, even if there are idle threads in the thread pool.
When the thread pool reaches corePoolSize, the newly submitted task will be placed in the workQueue, waiting for the task to be scheduled for execution in the thread pool
When the workQueue is full and maximumPoolSize > corePoolSize, the newly submitted task creates a new thread to execute the task
When the number of submitted tasks exceeds maximumPoolSize, the newly submitted task is handled by RejectedExecutionHandler
When corePoolSize threads are exceeded in the thread pool and idle time reaches keepAliveTime, shut down idle threads
When allowCoreThreadTimeOut (true) is set, the idle time of corePoolSize threads in the thread pool that reaches keepAliveTime will also be turned off
About the types of thread pools, differences and usage scenarios what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.