Summary of thread pool principles that must be asked in Java interviews

This article was first published from "MOOC". If you want to know more about IT dry goods and hot news in the programmer circle, welcome to pay attention to "MOOC"!

Author: Da Neng teacher | MOOC lecturer


Summary of Java thread pool principle

(1) What is a thread pool

The work of the thread pool is mainly to control the number of running threads. During the processing, tasks are put into the queue, and then these tasks are started after the threads are created. If the number of threads exceeds the maximum number, the excess threads wait in line and wait for other threads to execute. After completion, the task is taken out of the queue for execution.

His main features are:

  • Reduce resource consumption . Reduce the cost of thread creation and destruction by reusing created threads.
  • Improve responsiveness . When a task arrives, the task can be executed immediately without waiting for the thread to be created.
  • Improve thread manageability . Thread is a scarce resource. If it is created without limit, it will not only consume system resources, but also reduce the stability of the system. Using thread pool can be used for unified allocation, tuning and monitoring

(2) The basic principle of the thread pool

The composition of the thread pool The general thread pool is mainly divided into the following four components:

  1. Thread pool manager: used to create and manage thread pools

  2. Worker threads: threads in the thread pool

  3. Task interface: the interface that each task must implement, used by the worker thread to schedule its operation

  4. Task queue: used to store pending tasks and provide a buffer mechanism

The constructor of ThreadPoolExecutor is as follows:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) { }

Constructor parameter description:

parameter describe
corePoolSize Specifies the number of threads in the thread pool.
maximumPoolSize Specifies the maximum number of threads in the thread pool.
keepAliveTime When the number of current thread pools exceeds corePoolSize, the survival time of redundant idle threads, that is, they will be destroyed multiple times.
unit The unit of keepAliveTime.
workQueue Task queue, tasks that have been submitted but not yet executed.
threadFactory Thread factory, used to create threads, generally use the default one.
handler Rejection strategy, when there are too many tasks to deal with, how to reject tasks.

What kinds of work queues does the thread pool have?

1. ArrayBlockingQueue: It is a bounded blocking queue based on an array structure. This queue sorts elements according to the FIFO (first in first out) principle.

2. LinkedBlockingQueue: A blocking queue based on a linked list structure. This queue sorts elements by FIFO (first in first out), and the throughput is usually higher than that of ArrayBlockingQueue. The static factory method Executors.newFixedThreadPool() uses this queue

3. SynchronousQueue: A blocking queue that does not store elements. Each insertion operation must wait until another thread calls the removal operation, otherwise the insertion operation is always blocked, and the throughput is usually higher than LinkedBlockingQueue, which is used by the static factory method Executors.newCachedThreadPool.

4. PriorityBlockingQueue: An infinite blocking queue with priority.

Reject policy :

The threads in the thread pool have been used up and cannot continue to serve new tasks. At the same time, the waiting queue is full and no new tasks can be filled. At this time, we need to reject the policy mechanism to deal with this problem reasonably.

The built-in denial policies of the JDK are as follows:

Strategy describe
AbortPolicy An exception is thrown directly to prevent the system from running normally.
CallerRunsPolicy As long as the thread pool is not closed, this strategy runs the currently discarded task directly in the caller thread. Obviously doing this won't actually drop the task, but the performance of the task submitting thread will most likely drop dramatically.
DiscardOldestPolicy Discard the oldest request, that is, the task that is about to be executed, and try to submit the current task again.
DiscardPolicy This policy silently discards unprocessable tasks without any processing. This is the best solution if tasks are allowed to be lost.

The above built-in rejection strategies all implement the RejectedExecutionHandler interface. If the above strategies still cannot meet the actual needs, you can extend the RejectedExecutionHandler interface by yourself.

Java thread pool working process

  1. When the thread pool was first created, there was no thread in it. The task queue is passed in as a parameter. However, even if there are tasks in the queue, the thread pool will not execute them immediately.

  2. When calling the execute() method to add a task, the thread pool will make the following judgments:

    a) If the number of running threads is less than corePoolSize, create a thread to run the task immediately;

    b) If the number of running threads is greater than or equal to corePoolSize, then put this task into the queue;

    c) If the queue is full at this time, and the number of running threads is less than maximumPoolSize, then it is still necessary to create a non-core thread to run this task immediately;

    d) If the queue is full and the number of running threads is greater than or equal to maximumPoolSize, then the thread pool will throw an exception RejectExecutionException.

  3. When a thread completes a task, it takes the next task off the queue to execute.

  4. When a thread has nothing to do and exceeds a certain time (keepAliveTime), the thread pool will judge that if the number of currently running threads is greater than corePoolSize, then the thread will be stopped. So after all the tasks of the thread pool are completed, it will eventually shrink to the size of corePoolSize.

(3) Executor framework

The Executor framework was introduced in Java 5 with the java.util.concurrent.Executor interface. The Executor framework is a framework for asynchronous tasks that are invoked, scheduled, executed and controlled according to a set of execution policies.

Unlimited thread creation can cause the application to run out of memory. So creating a thread pool is a better solution, because the number of threads can be limited and these threads can be recycled and reused. Using the Executor framework can easily create a thread pool.

What is the Executor class?

Executors can be used to easily create thread pools.

The thread pool in Java is implemented through the Executor framework, which uses Executor, Executors, ExecutorService, ThreadPoolExecutor, Callable, Future, and FutureTask.

Shortcuts to creating common thread pools

It is more complicated to configure a thread pool, especially when the principle of the thread pool is not very clear, it is very likely that the configured thread pool is not optimal, so some static factories are provided in the Executors class to generate some commonly used The thread pool.

newSingleThreadExecutor

Create a single-threaded thread pool. This thread pool has only one thread working, which is equivalent to executing all tasks serially with a single thread. If the only thread ends abnormally, a new thread will take its place. This thread pool guarantees that the execution order of all tasks is executed in the order in which the tasks are submitted.

newFixedThreadPool

Create a fixed size thread pool. A thread is created each time a task is submitted, until the thread reaches the maximum size of the thread pool. Once the size of the thread pool reaches the maximum value, it will remain unchanged. If a thread ends due to an abnormal execution, the thread pool will be supplemented with a new thread.

newCachedThreadPool

Create a cacheable thread pool. If the size of the thread pool exceeds the number of threads required to process the task,

Then some idle threads (not executing tasks for 60 seconds) will be recycled. When the number of tasks increases, this thread pool can intelligently add new threads to process tasks. This thread pool does not limit the size of the thread pool, and the size of the thread pool is completely dependent on the maximum thread size that the operating system (or JVM) can create.

newScheduledThreadPool

Create a thread pool of unlimited size. This thread pool supports timing and periodic execution of tasks.

(4) Why it is not recommended to use Executors static factories to build thread pools

The Alibaba Java Development Manual clearly states that it is not allowed to use Executors static factories to build thread pools.
The reasons are as follows:
thread pools are not allowed to be created using Executors, but are created through ThreadPoolExecutor. This way of processing makes the writing students more clear about the operation of thread pools rules to avoid the risk of resource exhaustion

Explanation: The disadvantages of the thread pool object returned by Executors are as follows:

  • 1: FixedThreadPool and SingleThreadPool:
    The length of the allowed request queue (the underlying implementation is LinkedBlockingQueue) is Integer.MAX_VALUE, which may accumulate a large number of requests, resulting in OOM

  • 2: The number of threads allowed by CachedThreadPool and ScheduledThreadPool is Integer.MAX_VALUE, which may create a large number of threads, resulting in OOM.

 

Welcome to pay attention to the "MOOC" account. We will always insist on original content, provide high-quality content in the IT circle, and share dry knowledge. Let's grow together!
This article was originally published on Muke.com, please indicate the source for reprinting, thank you for your cooperation

Guess you like

Origin blog.csdn.net/mukewangguanfang/article/details/130503124