Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four thread pools that come with java?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the four thread pools that come with java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the four thread pools in java?"

Which four thread pools are predefined by java?

NewSingleThreadExexcutor: thread pool per thread (number of core threads = maximum number of threads = 1)

NewFixedThreadPool: thread pool with a fixed number of threads (core threads = maximum threads = custom)

NewCacheThreadPool: thread pool that can be cached (core threads = 0, maximum threads = Integer.MAX_VALUE)

NewScheduledThreadPool: thread pool that supports scheduled or periodic tasks (number of core threads = custom, maximum number of threads = Integer.MAX_VALUE)

What's the difference between the four thread pools?

The above four thread pool classes all inherit ThreadPoolExecutor and directly return new ThreadPoolExecutor (parameters) when they are created. The difference between them is that the parameters in the defined ThreadPoolExecutor (parameters) are different, while ThreadPoolExecutor inherits the ExecutorService interface class.

NewFixedThreadPool

Definition:

XecutorService executorService=Executors.newFixedThreadPool (2)

Disadvantages: using the chain phenotype blocking queue of LinkBlockQueue, when the stacking speed of tasks is faster than the processing speed, it is easy to pile up tasks and cause OOM memory overflow.

NewSingleThreadExecutor

Definition: ExecutorService executorService = Executors.newSingleThreadExecutor ()

The above code looks like new FixedThreadPoop (1), but it is different, because there is an extra layer of FinalizableDelegatedExecutorService on the outside, and its function is:

We know that the essence of fixedExecutorService is ThreadPoolExecutor, so fixedExecutorService can be forcibly converted to ThreadPoolExecutor, but singleExecutorService has nothing to do with ThreadPoolExecutor, so the overturn fails, so after newSingleThreadExecutor () is created, it can no longer modify its thread pool parameters and really single a single thread.

Disadvantages: using the chain phenotype blocking queue of LinkBlockQueue, when the stacking speed of tasks is faster than the processing speed, it is easy to pile up tasks and cause OOM memory overflow.

NewCacheThreadPool

Definition: ExecutorService executorService=Executors.newCacheThreadPool ()

Disadvantages: SynchronousQueue is an implementation of BlockingQueue, it is also a queue, because the maximum number of threads is Integer.MAX_VALUE, so it is easy to OOM memory overflow when there are too many threads.

ScheduledThreadPool

Definition: ExecutorService executorService=Executors.newScheduledThreadPool (2)

Source code: public static ScheduledExecutorService newScheduledThreadPool (int corePoolSize) {/ / ScheduledThreadPoolExecutor inherits ThreadPoolExecutor return new ScheduledThreadPoolExecutor (corePoolSize) } public ScheduledThreadPoolExecutor (int corePoolSize) {/ / ScheduledThreadPoolExecutor inherits ThreadPoolExecutor, so super () calls the constructor of ThreadPoolExecutor to initialize and return a ThreadPoolExecutor, while ThreadPoolExecutor makes the / / final ScheduledThreadPoolExecutor that implements the ExecutorService interface return ThreadPoolExecutor super (corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue ()), the implementation class of the ExecutorService interface, like the above thread pools;} what are the important parameters of the thread pool?

The construction method of ThreadPoolExecutor is as follows:

KeepAliveTime means that when the current number of threads is between [core threads, maximum threads], these non-core threads quit the thread pool when they wait for how long they are idle and have no work to do.

The size of the waiting column has nothing to do with the maximum number of threads. Thread creation priority = core thread > blocking queue > expanded thread (the current number of core threads is less than the maximum number of threads)

If the number of core threads is 5, the length of the waiting queue is 3, and the maximum number of threads is 10: when the number of threads is increasing, create 5 core threads first, and then throw the threads into the waiting queue when the number of core threads is full. When the waiting queue is full (3 threads), the maximum number of threads will be compared (only when the maximum number of threads is lost), you can continue to create 2 threads (5 threads 3 threads 2). If the number of threads exceeds the maximum number of threads, the reject policy is executed.

If the number of core threads is 5, the length of the waiting queue is 3, and the maximum number of threads is 7: when the number of threads is increasing, create 5 core threads first, and then throw the threads into the waiting queue when the number of core threads is full. When there are 2 threads in the waiting queue, it reaches the maximum number of threads (5 threads 2 threads 7), but the waiting queue is not full, so don't worry about the maximum number of threads until the waiting queue is full (3 blocking threads). At this point, the maximum number of threads will be compared (only waiting for the maximum number of threads to be lost). At this time, the core + waiting for the maximum number of threads = 53.8 > 7 = the maximum number of threads, that is, the maximum number of threads has been reached, then the reject policy will be implemented.

If the waiting drop column is set to LinkedBlockingQueue unbounded drop column, the drop column is infinite and will never come to the point of judging the maximum number of threads.

How to customize a thread pool

You can use bounded queues, custom threads to create factory ThreadFactory, and reject policy handler to customize thread pools

Public class ThreadTest {public static void main (String [] args) throws InterruptedException, IOException {int corePoolSize = 2; int maximumPoolSize = 4; long keepAliveTime = 10; TimeUnit unit = TimeUnit.SECONDS; BlockingQueue workQueue = new ArrayBlockingQueue (2); ThreadFactory threadFactory = new NameTreadFactory (); RejectedExecutionHandler handler = new MyIgnorePolicy (); ThreadPoolExecutor executor = new ThreadPoolExecutor (corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler) Executor.prestartAllCoreThreads (); / / pre-start all core threads for (int I = 1; I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report