In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "Java thread pool comprehensive knowledge point summary". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Principle
The principle of thread pool is very simple, which is summarized here in terms of processing flow:
The thread pool determines whether all the threads in the core pool are executing the task, and if not, create a new thread to execute the task.
If the core thread pool is full, store the new task in the work queue
If the work queue is full and the number of threads does not reach the thread pool limit, create a new thread to execute the task.
When the number of threads reaches the limit, a saturation policy is triggered to handle the task
Work queues are used to minimize the overhead of thread creation. Work queues are implemented with blocking queues.
Blocking queue
A blocking queue (BlockingQueue) is a queue that supports blocking insertion and removal of elements.
Blocking insertion: when the queue is full, block the thread that inserts the element until the queue is dissatisfied
Blocking removal: when the queue is empty, block the thread that removes the element until the queue is not empty
Principle: implemented using the notifier pattern. When a producer adds an element to a full queue, it blocks the producer. When a consumer removes an element, it notifies the producer that the current queue is available.
There are three types of blocking queues, which are:
Bounded blocking queues: ArrayBlockingQueue (array), LinkedBlockingQueue (linked list)
Unbounded blocking queues: LinkedTransferQueue (linked list), PriorityBlockingQueue (supports prioritization), DelayQueue (unbounded blocking queues that support delayed acquisition of elements)
Synchronous handover queue: SynchronousQueue
Bounded blocking queue
It mainly includes ArrayBlockingQueue (array) and LinkedBlockingQueue (linked list). The bounded queue size and the number of threads match each other. When the queue capacity is large and the number of threads is small, context switching can be reduced and cpu usage will be reduced, but throughput will be reduced.
Unbounded blocking queue
The more common one is LinkedTransferQueue. This is how FixedThreadPool is implemented. Unbounded blocking queues should be used with caution, because in some cases, it may cause a large number of tasks to pile up in the queue, causing memory to soar.
Synchronous handover queue
SynchronousQueue . Without storing the blocking queue of elements, each put operation must wait for a take operation, otherwise you cannot continue to add elements. Used to implement the CachedThreadPool thread pool.
The task queue mapping used by each thread pool is as follows:
Thread pool blocking queue
FixedThreadPoolLinkedBlockingQueueSingleThreadExecutorLinkedBlockingQueueCachedThreadExecutorSynchronousQueueScheduledThreadPoolExecutorLinkedBlockingQueue
Implementation class analysis
ThreadPoolExecutor is the implementation class of the Java thread pool and the core class derived from the Executor interface. The dependency diagram is as follows:
Here we have to mention the Executor framework, which consists of three parts, as follows:
Mission. Interfaces to be implemented for the task to be performed: Runnable and Callable
Mission carried out. That is, the above core interface Executor and the inherited ExecutorService. ExecutorService derives the following two classes: ThreadPoolExecutor: thread pool core implementation class; ScheduledThreadPoolExecutor: used to do timing tasks
The result of asynchronous calculation. Interface Future and the FutureTask class that implements the Future interface. Thread pool creation
New ThreadPoolExecutor (corePoolSize, maximumPoolSize, keepAliveTime, milliseconds, runnableTaskQueue, handler)
The construction method is as follows:
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.