public void Start(int userId, Bakery b) { Task.Factory.StartNew(() => { var r = new Random(); Console.WriteLine("Starting GoodUser {0}...", userId); b.Lock(userId); Console.WriteLine("Inside critical section {0}...", userId); // Do some silly work, and keep health upto date. for (var j=0; j < 100; j++) { Thread.Sleep(r.Next(0, 10)); } b.Unlock(userId); //throw new Exception(); Console.WriteLine("Released critical section {0}...", userId); }) .ContinueWith(t => { if (t.Exception != null) { Console.WriteLine("Oops!!!"); } }); }
public void Start(int userId, Bakery b) { Task.Factory.StartNew(() => { var r = new Random(); Console.WriteLine("Starting BadUser {0}...", userId); b.Lock(userId); Console.WriteLine("Inside critical section {0}...", userId); Thread.Sleep(TimeSpan.FromSeconds(r.Next(1, 3))); Console.WriteLine("Exiting without unlocking - FAILURE simulation..."); }); }