NewCachedThreadPool, the way to build a thread pool that comes with JDK

newCachedThreadPool acts as a cached thread pool.

NewCachedThreadPool internal code analysis

  • Looking at the comments above, you can know that this thread pool is created when the task is submitted for the first time.
  • If it is not used within 60 seconds after the task execution is completed, the thread will terminate.
  • If there is a task coming in within the 60 seconds of waiting, it will take the task to execute.
  • If there are more tasks and no thread is idle, a new thread will be built to perform the task.
  • The summary is that as long as the task comes to this thread pool, there must be a thread to execute it.

insert image description here

Demonstration of the effect of the code

  • Thread names are not repeated.

Guess you like

Origin blog.csdn.net/weixin_50503886/article/details/131318498