Thread Pool vs Thread Spawning

前端 未结 12 1641
梦如初夏
梦如初夏 2020-12-08 00:27

Can someone list some comparison points between Thread Spawning vs Thread Pooling, which one is better? Please consider the .NET framework as a reference implementation tha

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 01:08

    Thread pool threads are much cheaper than a regular Thread, they pool the system resources required for threads. But they have a number of limitations that may make them unfit:

    • You cannot abort a threadpool thread
    • There is no easy way to detect that a threadpool completed, no Thread.Join()
    • There is no easy way to marshal exceptions from a threadpool thread
    • You cannot display any kind of UI on a threadpool thread beyond a message box
    • A threadpool thread should not run longer than a few seconds
    • A threadpool thread should not block for a long time

    The latter two constraints are a side-effect of the threadpool scheduler, it tries to limit the number of active threads to the number of cores your CPU has available. This can cause long delays if you schedule many long running threads that block often.

    Many other threadpool implementations have similar constraints, give or take.

提交回复
热议问题