Last active
March 21, 2020 18:39
-
-
Save samueleresca/273f0660746de41c67d8d1b068896d9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Starvation | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(Environment.ProcessorCount); | |
ThreadPool.SetMinThreads(8, 8); | |
Task.Factory.StartNew( | |
Producer, | |
TaskCreationOptions.None); | |
Console.ReadLine(); | |
} | |
static void Producer() | |
{ | |
while (true) | |
{ | |
Process(); | |
Thread.Sleep(200); | |
} | |
} | |
static async Task Process() | |
{ | |
await Task.Yield(); | |
var tcs = new TaskCompletionSource<bool>(); | |
Task.Run(() => | |
{ | |
Thread.Sleep(1000); | |
tcs.SetResult(true); | |
}); | |
tcs.Task.Wait(); | |
Console.WriteLine("Ended - " + DateTime.Now.ToLongTimeString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment