任务与延续计划

时间:2016-04-21 06:44:07

标签: c# multithreading task-parallel-library task taskscheduler

假设我有许多CPU密集型任务被转储到默认调度程序上(所有这些任务一次运行,例如通过Task.RunTask.Factory.StartNew使用默认调度程序)。每项任务都有一个Continuation。调度程序将启动一些任务并将它们置于运行状态。当这些任务完成调度程序时,其队列中将包含原始任务和延续任务的混合以进行调度,并且必须在它们之间进行选择。它如何优先考虑延续与运行更多组织任务的优先级。具体来说,如果一个任务已经完成并且它有一个延续,那么继续在调度程序中优先于已经排队的其他任务。

例如,假设sheduler运行许多(T1 ... Tn)排队任务的2(T1和T2)。当其中一个完成时,调度程序必须运行延续C1或者它可能决定启动T3吗?无论如何它会选择哪种确定性?调度程序是否可能选择运行更多任务,并且因此在T1结束与其继续C1的开始之间可能存在相当大的延迟?

更新:我已经运行了一些示例代码并添加了一个答案 - 但是我仍然希望听到这种观察到的行为是否得到保证?

1 个答案:

答案 0 :(得分:0)

所以我尝试了一个样本:

var continuations = new List<Task>();
for (int i = 0; i < 20; i++) {
    int counter = i;
    var continuation = Task.Run(() => {
        Console.WriteLine($"T{counter}: Start.");
        Thread.Sleep(500);
        Console.WriteLine($"T{counter}: Complete.");
    }).ContinueWith(t => {
        Console.WriteLine($"C{counter}: Start.");
        Thread.Sleep(50);
        Console.WriteLine($"C{counter}: Complete.");
    });
    continuations.Add(continuation);
}
Task.WaitAll(continuations.ToArray());

其中输出以下内容 - 暗示调度程序确定优先级继续启动新任务(但这是否有保证?)

T1: Start.
T2: Start.
T3: Start.
T0: Start.
T1: Complete.
T2: Complete.
T3: Complete.
C1: Start.
C3: Start.
T0: Complete.
C0: Start.
C2: Start.
C1: Complete.
T4: Start.
C3: Complete.
T5: Start.
C0: Complete.
T6: Start.
C2: Complete.
T7: Start.
T4: Complete.
C4: Start.
T5: Complete.
C5: Start.
T6: Complete.
C6: Start.
T7: Complete.
C7: Start.
C4: Complete.
C5: Complete.
T9: Start.
T8: Start.
C7: Complete.
T10: Start.
C6: Complete.
T11: Start.
T8: Complete.
C8: Start.
T9: Complete.
C9: Start.
T11: Complete.
C11: Start.
T10: Complete.
C10: Start.
C9: Complete.
C8: Complete.
T13: Start.
T12: Start.
C11: Complete.
T14: Start.
C10: Complete.

变种。在启动任务和注册Continuation之间的调用线程上有额外的执行。

var continuations = new List<Task>();
for (int i = 0; i < 20; i++) {
    int counter = i;
    var task = Task.Run(() => {
        Console.WriteLine($"T{counter}: Start.");
        Thread.Sleep(500);
        Console.WriteLine($"T{counter}: Complete.");
    });
    Thread.Sleep(100); // Do some stuff before registering continuation.
    var continuation = task.ContinueWith(t => {
    Console.WriteLine($"C{counter}: Start.");
        Thread.Sleep(150);
        Console.WriteLine($"C{counter}: Complete.");
    });
    continuations.Add(continuation);
}
Task.WaitAll(continuations.ToArray());

结果与上述情况相同,即任务N的延续优先于其他排队任务。

T0: Start.
T1: Start.
T2: Start.
T3: Start.
T0: Complete.
C0: Start.
T1: Complete.
C1: Start.
C0: Complete.
T4: Start.
T2: Complete.
C2: Start.
C1: Complete.
T5: Start.
T3: Complete.
C3: Start.
C2: Complete.
T6: Start.
C3: Complete.
T7: Start.
T4: Complete.
C4: Start.
T5: Complete.
C5: Start.
C4: Complete.
T8: Start.
T6: Complete.
C6: Start.
C5: Complete.
T9: Start.
T7: Complete.
C7: Start.
C6: Complete.
T10: Start.
C7: Complete.
T11: Start.
T8: Complete.
C8: Start.
T9: Complete.
C9: Start.
C8: Complete.
T12: Start.
T10: Complete.
C10: Start.
C9: Complete.
T13: Start.
T11: Complete.
C11: Start.
T14: Start.
C10: Complete.
T15: Start.
C11: Complete.
T16: Start.
T12: Complete.
C12: Start.
T13: Complete.
C13: Start.
T14: Complete.
C14: Start.
C12: Complete.
T17: Start.
T15: Complete.
C15: Start.......

另一种变化。在注册延续之前,所有任务首先在scheuler上排队(并且可以安排运行)。

const int taskCount = 20;
var tasks = new List<Task>();
for (int i = 0; i < taskCount; i++) {
    int counter = i;
    var task = Task.Run(() => {
        Console.WriteLine($"T{counter}: Start.");
        Thread.Sleep(500);
        Console.WriteLine($"T{counter}: Complete.");
    });
    tasks.Add(task);
}
Thread.Sleep(400);
var continuations = new List<Task>();
for (int i = 0; i < taskCount; i++) {
    int counter = i;
    var continuation = tasks[i].ContinueWith(t => {
        Console.WriteLine($"C{counter}: Start.");
        Thread.Sleep(150);
        Console.WriteLine($"C{counter}: Complete.");
    });
    continuations.Add(continuation);
}
Task.WaitAll(continuations.ToArray());

结果与以前一样 - 延续获得优先权。

T0: Start.
T2: Start.
T3: Start.
T1: Start.
T1: Complete.
T3: Complete.
T0: Complete.
T2: Complete.
C2: Start.
C0: Start.
C3: Start.
C1: Start.
C2: Complete.
C1: Complete.
C0: Complete.
C3: Complete.
T7: Start.
T4: Start.
T6: Start.
T5: Start.
T6: Complete.
T5: Complete.
T7: Complete.
T4: Complete.
C6: Start.
C5: Start.
C7: Start.
C4: Start.
C7: Complete.
T8: Start.
C5: Complete.
T9: Start.
C6: Complete.
T10: Start.
C4: Complete.
T11: Start.
T8: Complete.
C8: Start.
T10: Complete.
C10: Start.
T9: Complete.
C9: Start.
T11: Complete.
C11: Start.
C8: Complete.
T12: Start.
C9: Complete.
T13: Start.
C10: Complete.
T14: Start.
C11: Complete.
T15: Start.
T14: Complete.
T12: Complete.
C12: Start.
T13: Complete.
C13: Start.
C14: Start.
T15: Complete.
C15: Start.
T16: Start.
C12: Complete.
C13: Complete.
T18: Start.
T17: Start.
C14: Complete.
T19: Start.
C15: Complete.
T16: Complete.
C16: Start.
T18: Complete.
C18: Start.
T17: Complete.
T19: Complete.
C19: Start.
C16: Complete.
C17: Start.
C18: Complete.
C19: Complete.
C17: Complete.