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

后端 未结 3 705
野的像风
野的像风 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:11

    The point of the threadpool is to avoid the situation where the time spent creating the thread is longer than the time spent using it. By reusing existing threads, we get to avoid that overhead.

    The downside is that the threadpool is a shared resource: if you're using a thread, something else can't. So if you have lots of long-running tasks, you could end up with thread-pool starvation, possibly even leading to deadlock.

    Don't forget that your application's code may not be the only code using the thread pool... the system code uses it a lot too.

    It sounds like you might want to have your own producer/consumer queue, with a small number of threads processing it. Alternatively, if you could talk to your other service using an asynchronous API, you may find that each bit of processing on your computer would be short-lived.

提交回复
热议问题