# The Orleans Scheduler

Draft
Orleans
Table of Contents

Interleaving

Each activation (instance of a grain) has its own TaskScheduler which ensures that any tasks spawned in that activation abide by the threading rules, namely that there is only a single thread executing in the context of an activation at any time. That’s not a technical limitation, it’s very much a design choice.

// This method is marked [AlwaysInterleave] in the grain interface
public Task<string> PerformComputations()
{
// No other code will execute on your grain while this is executing (there are no awaits in there)
Guid specialValue = this.CalculateSpecialValue();
await Task.Delay(1000); // During this 1000ms, other [AlwaysInterleave] calls can execute
// No other code will execute on your grain while this is executing (there are no awaits)
var x = 0;
for (var i = 0; i < 100; i++)
{
// Do some meaningless stuff
x += DateTime.UtcNow.Milliseconds;
}
var otherGrain = this.GrainFactory.GetGrain<IOtherGrain>(specialValue);
var stopWatch = Stopwatch.StartNew();
await otherGrain.Ping(); // Other [AlwaysInterleave] methods can execute while we are waiting for a response.
var pingTime = stopWatch.ElapsedMilliseconds;
return $"It took {pingTime}ms to ping the other grain!";
}