简体   繁体   English

有没有办法修复任务计划运行的线程数?

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

Say I create 3 threads, but wish to run 4 Tasks on them. 假设我创建了3个线程,但希望在它们上运行4个任务。 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. 使用任务(TPL)时,不创建线程。

Almost every method in the Parallel class will allow you to specify WithDegreeOfParallelism(n) 几乎所有Parallel类中的方法都允许你指定WithDegreeOfParallelism(n)

For details, see Setting the cores to use in Parallelism 有关详细信息,请参阅设置要在Parallelism中使用的核心

You could also create your own TaskScheduler ... if you really need to. 你也可以创建自己的TaskScheduler ...... 如果你真的需要的话。 Not sure what your particular use case is for it though from your question. 从您的问题来看,不确定您的特定用例是什么。 See this link on MSDN for more information. 有关详细信息,请参阅MSDN上的链接。

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. 在TPL中,您不创建线程,只创建任务,如果您使用的是默认调度程序,它将在后端使用Threadpool,默认情况下最大线程数为2 *核心数。 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 如果您要管理线程创建,可以通过从TaskScheduler驱动来创建自己的自定义sccheduler,此MSDN页面显示如何实现限制并发度的自定义调度程序http://msdn.microsoft.com/en-us/library/ee789351的.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM