Why .net Threadpool is used only for short time span tasks?

后端 未结 3 703
野的像风
野的像风 2021-02-09 04:55

I\'ve read at many places that .net Threadpool is meant for short time span tasks (may be not more than 3secs). In all these mentioning I\'ve not found a concrete reason why it

3条回答
  •  无人共我
    2021-02-09 05:04

    Two reasons not really touched upon:

    1. The threadpool is used as the normal means of handling I/O callback functions, which are usually supposed to happen very soon after associated I/O operation completes. In general, timeliness is more important with short tasks than long ones, but long-running tasks in the threadpool will delay the execution of notification tasks which could have (and should have) started up, run, and completed quickly.
    2. If a threadpool task becomes blocked until such time as some other threadpool task runs, it may hog a threadpool thread, thus delaying or in some cases blocking altogether the start of that other task (or any others).

    Generally, having a threadpool thread acquire a lock (waiting if necessary) isn't a problem. If it's necessary for one threadpool thread to wait for another threadpool thread to release a lock, the fact that latter thread acquired the lock in the first place implies that it got started. On the other hand, waiting for e.g. some data to arrive from a connection may cause deadlock if an I/O callback routine is used to flag the arrival of data. If many too many threadpool threads are waiting for the I/O callback to signal that data has arrived, the system may decide to defer the callback until one of the threadpool threads completes.

提交回复
热议问题