JDK's own way to build a thread pool: newWorkStealingPool

The creation method of newWorkStealingPool is very different from the previous thread pools. The four thread pools of fixed length, single instance, cache, and scheduled task are all implemented based on ThreadPoolExecutor . newWorkStealingPool is built based on ForkJoinPool .

The core features of ThreadPoolExecutor

There is only one blocking queue DelayedWorkQueue used to store the current task. In the figure below, it is obvious that four tasks are blocked and waiting in this queue. If a task comes, it will be consumed by one of the threads.
insert image description here

Core Features of ForkJoinPool

Forked connection pool. When there is a particularly large task, ForkJoinPool is different from the above four thread pools in the way that a large task is handed over to a certain thread for execution. The feature of the ForkJoinPool thread pool is to split this large task into multiple small tasks and put them in the blocking queue of the current thread. Other threads can then process thread tasks in different blocking queues.
insert image description here

Code effect demo

  • Single-threaded effect demonstration
    insert image description here
  • Demonstration of the effect of multithreading
    insert image description here

When splitting tasks, the more the better. The task volume must be large, and it will take a long time!

Guess you like

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