The difference between the thread pool and multi-threaded

The concept of the thread pool

Thread Pool classified into four categories in total

  • fixThreadPool regular thread (traditional thread pool)
  • cacheThreadPool cached thread pool
  • singleThreadPoll single-threaded thread pool (singleton thread pool)
  • ScheduledThreadPoll periodically perform a task thread pool
fixThreadPool regular thread (traditional thread pool)

Contains kernel threads, kernel threads is the maximum number of threads, no non-core thread

cacheThreadPool cached thread pool

Cacheable thread pool, a large maximum number of threads, it will add a new thread for each task, here are a time-out mechanism, when the idle threads not used over a certain time, it will be recycled.

singleThreadPoll single-threaded thread pool (singleton thread pool)

There is only one thread, through a predetermined order one by one task thread thrown queued for execution, without concurrent processing operations, will not be recovered.

ScheduledThreadPoll periodically perform a task thread pool

Periodic execution thread pool tasks, in accordance with a specific plan task execution thread, there is a core thread, but there are also non-core thread, the size of the non-core thread is also infinite. Adapted to perform periodic tasks.

The difference between the thread pool with multiple threads

  • The thread pool is in the running to start creating good n threads, and these n thread hangs waiting for the arrival of the task. The multi-threaded tasks are created in the arrival time was, and then perform the task.
  • Thread pool thread will not be recovered after the execution of the thread, the thread will continue to wait in a queue; multithreaded program after each task is completed will recover the thread.
  • Since the thread pool is to create good, so the efficiency with respect to multi-thread would be much higher.
  • It has a good performance also in the case of high concurrency of the thread pool; not easy to hang. Multithreading in more number of threads to create a situation, it is easy to hang.
Published 52 original articles · won praise 26 · views 3386

Guess you like

Origin blog.csdn.net/weixin_43796685/article/details/104926905