Thread.Start() versus ThreadPool.QueueUserWorkItem()

前端 未结 7 2122
长发绾君心
长发绾君心 2020-12-12 16:20

The Microsoft .NET Base Class Library provides several ways to create a thread and start it. Basically the invocation is very similar to every other one providing the same k

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 16:59

    Starting a new thread can be a very expensive operation. The thread pool reuses threads and thus amortizes the cost. Unless you need a dedicated thread, the thread pool is the recommended way to go. By using a dedicated thread you have more control over thread specific attributes such as priority, culture and so forth. Also, you should not do long running tasks on the thread pool as it will force the pool to spawn additional threads.

    In addition to the options you mention .NET 4 offers some great abstractions for concurrency. Check out the Task and Parallel classes as well as all the new PLINQ methods.

提交回复
热议问题