开发者

Is there a way to fix the number of threads tasks will schedule to run on?

开发者 https://www.devze.com 2023-03-29 22:48 出处:网络
Say I create 3 threads, but wish to run 4 Tasks on them. Is there any way开发者_运维问答 to fix the number of threads that tasks will run on? Thanks.When you use Tasks (TPL), you do not create the Thr

Say I create 3 threads, but wish to run 4 Tasks on them. Is there any way开发者_运维问答 to fix the number of threads that tasks will run on? Thanks.


When you use Tasks (TPL), you do not create the Threads.

Almost every method in the Parallel class will allow you to specify WithDegreeOfParallelism(n)

For details, see Setting the cores to use in Parallelism


You could also create your own TaskScheduler... if you really need to. Not sure what your particular use case is for it though from your question. See this link on MSDN for more information.


In TPL you don't create Threads, you only create Tasks and if you are using the de default scheduler it will use the Threadpool in the back end, and the max number of threads by default is 2*number of cores. You could create your own custom sccheduler by driving from TaskScheduler if you ant to manage threads creation, this MSDN page shows how to implement custom scheduler that limits the concurrency degree http://msdn.microsoft.com/en-us/library/ee789351.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消