I'm reading through the Exam Ref 70-483: Programming in C# book and the following code sample is given:
LISTING 1-19
public Task SleepAsyncA(int millisecondsTimeout)
{
return Task.Run(() => thread.Sleep(millisecondsTimeout);
}
public Task SleepAsyncB(int millisecondsTimeout)
{
TaskCompletionSource<bool> tcs = null;
var t = new Timer(delegate { tcs.TrySetResult(true); }, -1, -1);
tcs = new TaskCompletionSource<bool>(t);
t.Change(millisecondsTimeout, -1);
return tcs.Task;
}
The paragraph under states this:
The
SleepAsyncAmethod uses a thread from the thread pool while sleeping. the second method, however, which has a completely different implementation, does not occupy a thread while waiting for the timer to run. The second method gives you scalability.
Why is A responsive but B scalable?