Is it true that for long running processes it is better to do thread manually instead of threadpool?

前端 未结 2 1499

I read on the other day that for long-running tasks my best bet is to manually create threads instead of using .NET’s thread pool or Task Parallel. I\'d really like someone

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 11:39

    That is true. The thread pool is optimised for small units of work and you can interfere with other work by holding onto a thread pool thread.

    My rule of thumb is if an operation can take more than a second, it should not be on a thread pool thread. That is probably quite long.

    Although this is undocumented, if you start a Task with TaskCreationOptions.LongRunning then a new Thread will be started to run the Task.

    For most IO tasks, there are asynchronous versions of the framework methods that you should really use. These make use of kernel functions and mean that you won't be blocking any thread.

    As always, I recommend reading Joe Albahari's free ebook, followed by Joe Duffy's Concurrent Programming on Windows. The later is 1000 pages long, but is full of useful details.

提交回复
热议问题