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.
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.
Code effect demo
- Single-threaded effect demonstration
- Demonstration of the effect of multithreading
When splitting tasks, the more the better. The task volume must be large, and it will take a long time!