[Java Basics] Java Thread Pool

Keywords: Java

First let's think about why we use thread pools.Here are the main reasons why:

  1. If you create threads very frequently, each processing time will be very short, and our frequent creation and destruction of threads can incur enormous overhead.
  2. Secondly, if we create a thread every time we need one, it would be inconvenient for us to manage the thread.

These are the factors we need to consider when developing to use thread pools.

Java uses Executors to provide four thread pools through a static factory method:

Executors.newCachedThreadPool() creates a pool of threads with no size limitations and is created as soon as a new thread needs to be created.If each thread is within 60 seconds
 Unhandled will be destroyed.
Executors.newFixedThreadPool() creates a fixed-size thread pool.
Executors.newScheduledThreadPool() creates a thread pool for deferred execution.
Executors.newSingleThreadExecutor() creates a thread pool with only one thread. 

Looking at the implementations of the above methods, you will see that a ThreadPoolExecutor object has been created.Below we analyze the implementation of these static factory methods for Executors.

First, let's look at the constructor for ThreadPoolExecutor():

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}
corePoolSize: Number of threads in the thread pool that will not be recycled even if they are idle
maximumPoolSize: Maximum number of threads allowed in thread pool
keepAliveTime: Exceed corePoolSize Maximum lifetime of number of threads
unit: KeepAliveTime Unit of time
workQueue: For storage execute Method Submitted Tasks

newCachedThreadPool()

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

We can notice that newCachedThreadPool() creates a thread pool with an initial number of threads of 0, a maximum number of threads of integer type in the thread pool, and an idle thread lifetime of 60 seconds.

newFixedThreadPool()

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

Also note that newFixedThreadPool() creates a thread pool with the same number of initial and maximum threads (nThreads).

newScheduledThreadPool()

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

new ScheScheduledThreadPoolExecutor(corePoolSize)The implementation is as follows:
public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue());
}
    

newScheduledThreadPool() first calls ScheduledThreadPoolExecutor(), then ScheduledThreadPoolExecutor() calls the ThreadPoolExecutor() constructor.A thread pool was created where the initial number of threads was corePoolSize and the maximum number of threads was an integer, exceeding the immediate destruction of idle threads in corePoolSize.

newSingleThreadExecutor()

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}

From the code above, we can see that newSingleThreadExecutor() created a thread with a number of initial and maximum threads of 1.

Read here and you may wonder how the thread pool created by newScheduledThreadPool() achieves latency.
Not Ended to Continue

Posted by JeBu on Thu, 22 Aug 2019 19:52:41 -0700