奥尔良任务的交织

时间:2019-02-04 09:15:20

标签: c# asp.net actor orleans

有一个问题here,其中讨论了在ContinueWith的上下文中执行Orleans任务。我对该示例进行了一些修改,以检查是否可以在不使用[Reentrant]标志的情况下使两个奥尔良相同任务的任务交错。整个代码为here。该代码的重点如下:

// Client code
var hashGenerator = client.GetGrain<IHashGeneratorGrain>(0);
Console.WriteLine("Client making a call");
hashGenerator.Call_A_ToTemp();
await Task.Delay(10000);
hashGenerator.Call_B_ToTemp();


// HashGeneratorGrain
public async Task Call_A_ToTemp()
{
    Console.WriteLine("Making call A to a fellow grain");
    ITempGrain grain = this.GrainFactory.GetGrain<ITempGrain>(1);

    // CallA() returns successfully in 20s that is less than the timeout.
    grain.CallA().ContinueWith(async (t)=> 
    {
       if (t.IsCompletedSuccessfully)
       {
          Console.WriteLine("Task success");
          Console.WriteLine("Call_A_ToTemp: Counter value now is: " + counter);
          var selfGrain = this.GrainFactory.GetGrain<IHashGeneratorGrain>(0);
          await selfGrain.IncCounter();
          Console.WriteLine("Call_A_ToTemp: after increment: " + this.counter);
       }
    }
    );
}

public async Task IncCounter() 
{
   this.counter += 1;
}

public async Task Call_B_ToTemp()
{
   Console.WriteLine("Call_B_ToTemp called");
   Console.WriteLine("Call_B_ToTemp: Counter value now is: " + counter);
   Console.WriteLine("Call_B_ToTemp: now sleep for 20s");
   await Task.Delay(20000);
   Console.WriteLine("Call_B_ToTemp: after sleep value: " + counter);
   this.counter += 1;
   Console.WriteLine("Call_B_ToTemp: after increment: " + this.counter);
}

// TempGrain
public async Task CallA()
{
    Console.WriteLine("Call A came to TempGrain");
    await Task.Delay(20000);
}

输出为:

Client making a call
Making call A to a fellow grain
Call A came to TempGrain
Call_B_ToTemp called
Call_B_ToTemp: Counter value now is: 0
Call_B_ToTemp: now sleep for 20s
Task success
Call_A_ToTemp: Counter value now is: 0
Call_A_ToTemp: after increment: 1
Call_B_ToTemp: after sleep value: 1
Call_B_ToTemp: after increment: 2

如我们所见,Call_B_ToTemp()尚未完成,但是IncCounter()在退出之间和退出之后被调用,Call_B_ToTemp()被执行并完成。

这不违反奥尔良谷物的单螺纹性吗?

0 个答案:

没有答案