.Net TPL:具有任务优先级的有限并发级别任务调度程序?

时间:2023-01-25 02:11:53

I am currently using the LimitedConcurrencyLevelTaskScheduler detailed here http://msdn.microsoft.com/en-us/library/ee789351.aspx

我目前正在使用这里详细介绍的LimitedConcurrencyLevelTask​​Scheduler http://msdn.microsoft.com/en-us/library/ee789351.aspx

I want to enhance this so that individuals tasks can be assigned priority. These priorities need not map to thread priority. It should only influence the order in which tasks are started.

我希望对此进行增强,以便为个人任务分配优先级。这些优先级不需要映射到线程优先级。它应该只影响任务的启动顺序。

Does anyone know of an example of such a task scheduler? (a lot of the scheduling stuff is over my head so it would be great if there was an existing solution)

有谁知道这样一个任务调度程序的例子? (很多调度的东西都在我头上,所以如果有现有的解决方案会很棒)

2 个解决方案

#1


39  

The Parallel Extensions Extras Samples. already provide such a scheduler, the QueuedTaskScheduler. This scheduler provides priorities, concurrency limits, fairness and fine-grained control over the type and priorities of the threads used. Of course, you don't have to use or configure the features you don't need.

Parallel Extensions Extras Samples。已经提供了这样的调度程序QueuedTaskScheduler。此调度程序提供优先级,并发限制,公平性和对所用线程的类型和优先级的细粒度控制。当然,您不必使用或配置您不需要的功能。

Stephen Toub provides a brief description of the various schedulers in the Parallel Extensions Extras here

Stephen Toub在此提供了Parallel Extensions Extras中各种调度程序的简要说明

To use the QueuedTaskScheduler, you call its ActivateNewQueue method with the priority you need. This method returns a new TaskScheduler-derived Queue object managed by the parent TaskScheduler. All tasks that use a specific queue are scheduled by the parent TaskScheduler according to their priorities.

要使用QueuedTaskScheduler,可以使用所需的优先级调用其ActivateNewQueue方法。此方法返回由父TaskScheduler管理的新TaskScheduler派生的Queue对象。所有使用特定队列的任务都由父TaskScheduler根据其优先级进行调度。

The following code creates a scheduler with a maximum concurrency level of 4, two priority queues and schedules a task on the first queue:

以下代码创建一个最大并发级别为4的调度程序,两个优先级队列并在第一个队列上调度任务:

QueuedTaskScheduler qts = new QueuedTaskScheduler(TaskScheduler.Default,4);
TaskScheduler pri0 = qts.ActivateNewQueue(priority: 0);
TaskScheduler pri1 = qts.ActivateNewQueue(priority: 1);

Task.Factory.StartNew(()=>{ }, 
                      CancellationToken.None, 
                      TaskCreationOptions.None, 
                      pri0);

#2


1  

Use some sorted or priority data structure for the task list. Then create your own add that takes in the Priority. This may not be as good as others but it will prioritize Tasks List. You can reuse 99% of the code there. Simply replace LinkedList with a Sorted list or use LINQ to sort and write a method add that takes the priority.

对任务列表使用某些排序或优先级数据结构。然后创建自己的添加,其中包含优先级。这可能不如其他人好,但它会优先考虑任务列表。您可以在那里重用99%的代码。只需将LinkedList替换为Sorted列表,或使用LINQ对具有优先级的方法add进行排序和编写。

#1


39  

The Parallel Extensions Extras Samples. already provide such a scheduler, the QueuedTaskScheduler. This scheduler provides priorities, concurrency limits, fairness and fine-grained control over the type and priorities of the threads used. Of course, you don't have to use or configure the features you don't need.

Parallel Extensions Extras Samples。已经提供了这样的调度程序QueuedTaskScheduler。此调度程序提供优先级,并发限制,公平性和对所用线程的类型和优先级的细粒度控制。当然,您不必使用或配置您不需要的功能。

Stephen Toub provides a brief description of the various schedulers in the Parallel Extensions Extras here

Stephen Toub在此提供了Parallel Extensions Extras中各种调度程序的简要说明

To use the QueuedTaskScheduler, you call its ActivateNewQueue method with the priority you need. This method returns a new TaskScheduler-derived Queue object managed by the parent TaskScheduler. All tasks that use a specific queue are scheduled by the parent TaskScheduler according to their priorities.

要使用QueuedTaskScheduler,可以使用所需的优先级调用其ActivateNewQueue方法。此方法返回由父TaskScheduler管理的新TaskScheduler派生的Queue对象。所有使用特定队列的任务都由父TaskScheduler根据其优先级进行调度。

The following code creates a scheduler with a maximum concurrency level of 4, two priority queues and schedules a task on the first queue:

以下代码创建一个最大并发级别为4的调度程序,两个优先级队列并在第一个队列上调度任务:

QueuedTaskScheduler qts = new QueuedTaskScheduler(TaskScheduler.Default,4);
TaskScheduler pri0 = qts.ActivateNewQueue(priority: 0);
TaskScheduler pri1 = qts.ActivateNewQueue(priority: 1);

Task.Factory.StartNew(()=>{ }, 
                      CancellationToken.None, 
                      TaskCreationOptions.None, 
                      pri0);

#2


1  

Use some sorted or priority data structure for the task list. Then create your own add that takes in the Priority. This may not be as good as others but it will prioritize Tasks List. You can reuse 99% of the code there. Simply replace LinkedList with a Sorted list or use LINQ to sort and write a method add that takes the priority.

对任务列表使用某些排序或优先级数据结构。然后创建自己的添加,其中包含优先级。这可能不如其他人好,但它会优先考虑任务列表。您可以在那里重用99%的代码。只需将LinkedList替换为Sorted列表,或使用LINQ对具有优先级的方法add进行排序和编写。