コード例 #1
0
ファイル: GoodUser.cs プロジェクト: mjyeaney/BakeryAlgorithm
        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!!!");
                    }
                });
        }
コード例 #2
0
ファイル: BadUser.cs プロジェクト: mjyeaney/BakeryAlgorithm
        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...");
                });
        }