[JUC source code] thread pool: ThreadPoolExecutor (5) summary

Thread pool series:

After analyzing the source code of ThreadPoolExecutor in the previous articles, we will summarize the key information of ThreadPoolExecutor:

1) The thread pool solves two problems:

  • By reducing the scheduling overhead between tasks (mainly by the way the threads in the thread pool are reused), to improve the execution performance of a large number of tasks
  • Provides a way to manage threads and consumption, maintain basic data statistics and other tasks, such as counting the number of completed tasks;

2) Thread pool capacity related parameters:

  • coreSize: When a new task is submitted, it is found that the number of running threads is less than coreSize, a new thread will be created, even if other worker threads are idle at this time, coreSize can be obtained through the getCorePoolSize method

  • maxSize: When the task is submitted, coreSize <number of running threads <= maxSize, but when the queue is not full, the task is submitted to the queue. If the queue is full, create a new thread within the range allowed by maxSize;

    Generally speaking, coreSize and maxSize are already set when the thread pool is initialized, but we can also dynamically modify these two values ​​through the setCorePoolSize and setMaximumPoolSize methods;

3) Keep-alive times parameter:

  • Function: If there are threads exceeding coreSize in the current thread pool, and the idle time of the threads exceeds keepAliveTime, the current thread will be recycled, which can avoid the waste of resources when the thread is not used;
  • The value of keepAliveTime can be dynamically set through the setKeepAliveTime method;
  • If allowCoreThreadTimeOut is set to true, the core thread will be recycled if its idle time exceeds keepAliveTime;

4) There are many queue choices when creating a thread pool, such as:

  • ArrayBlockingQueue, a bounded queue, can prevent resources from being exhausted;
  • LinkedBlockingQueue, unbounded queue, unconsumed tasks can wait in the queue
  • SynchronousQueue, in order to avoid task rejection, requires the maxSize of the thread pool to be unbounded. The disadvantage is that when the speed of task submission exceeds the speed of consumption, unlimited thread growth may occur

5) Rejection strategy: When the Executor has been closed or the maximum thread and maximum queue are both saturated, the RejectedExecutionHandler class can be used to capture exceptions. There are four processing strategies as follows:

  • AbortPolicy (default): throw an exception
  • CallerRunsPolicy: do not use the thread pool, the main thread to execute
  • DiscardPolicy: directly discard the task
  • DiscardOldestPolicy: Discard the oldest task in the queue

6) ExecutorService uses threads in the thread pool to execute the submitted tasks. The thread pool can be configured using Executors. Executors sets a method to directly initialize the thread pool for common scenarios, such as:

  • Executors#newCachedThreadPool Unbounded thread pool, and can be automatically recycled
  • Executors#newFixedThreadPool fixed size thread pool
  • Executors#newSingleThreadExecutor single thread thread pool;

7) In addition, the thread pool provides many hook functions that can be extended, such as:

  • Provide hook methods for beforeExecute and afterExecute before each task is executed, mainly used to operate the execution environment, such as initializing ThreadLocals, collecting statistics, adding log entries, etc.
  • If you want to do something after the execution of the executor is completed, you can implement the terminated method. If an exception occurs during the execution of the hook method, the worker thread may fail and terminate immediately.

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/113965332