Semi-synchronous / semi thread pool reactors

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/LF_2016/article/details/72794814

First, what is the thread pool
thread pool is a group of pre-created server threads, these threads run the same code, and have the same attributes. When a new task comes, the main thread pool thread selection in some way one thread to serve, then serve again and again back into the thread pool.

Second, why have thread pool
compared to the dynamic creation of threads, the thread pool can improve the speed. Because the thread creation and destruction takes time, if recycled, then after our pre-created, used up, it will save a lot of time. And the number of CPU threads in the thread pool is generally similar, otherwise will cause the system to spend a lot of time on switching between threads.

Third, the semi-synchronous / semi reactor thread pool to achieve
semi-synchronous / semi thread pool reactors principle:
Write pictures described here

Synchronized via a shared work queues between the main thread and the worker thread, the worker thread work queue sleep. When a new task arrival, the main thread will add a new task to the work queue. This will wake up the worker thread is waiting for the task, but only one worker thread will get "the right to take over the" new tasks, he can be removed from the work queue and the task execution. The other worker threads continue to sleep on the job queue.
Because between the main thread and a worker thread work queue, so there is no coupling between the main thread and the worker thread, the main thread is inserted into the work queue task, the worker thread to get sleep through competitive tasks and execute it.

Thread pool source

Fourth, examples of
realization of the thread pool version of the web server:
thread pool version of the web server

Guess you like

Origin www.cnblogs.com/xjyxp/p/11488895.html