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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to create a thread pool through ThreadPoolExecutor? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Thread pool is not recommended to use Executors to create, but through the way of ThreadPoolExecutor, which makes the students more clear about the running rules of thread pool and avoid the risk of resource exhaustion.
Disadvantages of each method of Executors:
NewFixedThreadPool and newSingleThreadExecutor:
The main problem is that stacked request processing queues can consume a lot of memory, even OOM. (author's note: all blocking queues use LinkedBlockingQueue)
NewCachedThreadPool and newScheduledThreadPool:
The main problem is that the maximum number of threads is Integer.MAX_VALUE, which may create a very large number of threads, or even OOM.
ThreadPoolExecutor function
Executors provides four ways to create thread pools. In fact, ThreadPoolExecutor is also called at the bottom of Executors. The function is defined as follows:
Public ThreadPoolExecutor (int corePoolSize, / / the number of core threads in the thread pool int maximumPoolSize, / / the maximum number of threads in the thread pool long keepAliveTime, / / when the number of threads is greater than the core, extra idle threads wait for the survival time of the new task. TimeUnit unit, / / keepAliveTime unit of time ThreadFactory threadFactory, / / Thread factory BlockingQueue workQueue,// is used to store the queue RejectedExecutionHandler handler / / reject policy waiting for the task to be executed)
How thread pools work:
Supplementary explanation of function parameters
WorkQueue has the following seven options:
ArrayBlockingQueue: a bounded blocking queue consisting of array structures (array structures can be used with pointers to implement a circular queue).
LinkedBlockingQueue: a bounded blocking queue consisting of linked list structures, which defaults to Integer.MAX_VALUE when no capacity is specified.
PriorityBlockingQueue: an unbounded blocking queue that supports prioritization. There are no requirements for elements. You can implement the Comparable interface or provide Comparator to compare the elements in the queue. It has nothing to do with time, but just takes tasks according to priority.
DelayQueue: like PriorityBlockingQueue, it is also a priority primary blocking queue implemented by the binary heap. All the elements are required to implement the Delayed interface, and the task is extracted from the queue through the execution delay, and the task cannot be extracted until the time is up.
SynchronousQueue: a blocking queue that does not store elements, the consumer thread will block when it calls the take () method until a producer thread produces an element, the consumer thread can get the element and return; the producer thread will block when the put () method is called, and the producer will not return until a consumer thread consumes an element.
LinkedTransferQueue: it's a combination of ConcurrentLinkedQueue, LinkedBlockingQueue, and SynchronousQueue, but using it in ThreadPoolExecutor is consistent with LinkedBlockingQueue behavior.
LinkedBlockingDeque: a double-ended blocking queue implemented using a two-way queue, which means that it can be FIFO like a normal queue and FILO like a stack.
There are four values for handler:
AbortPolicy (default): discards the task and throws a RejectedExecutionException exception.
CallerRunsPolicy: the task is handled by the calling thread. (for example, for io operations, thread consumption is not as fast as NIO reads, which may cause blocking queues to increase all the time. This mode can be used in this case)
DiscardPolicy: discards the task, but does not throw an exception. (custom handling can be done in conjunction with this mode)
DiscardOldestPolicy: discard the earliest outstanding task in the queue, and then try to execute the task again (repeat execution)
This is the answer to the question on how to create a thread pool through ThreadPoolExecutor. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.