Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             new Thread(FastClient1_OnId1).Start();
  14.             new Thread(FastClient2_OnId1).Start();
  15.             new Thread(SlowClient1_OnId2).Start();
  16.  
  17.             Console.ReadKey();
  18.         }
  19.  
  20.         /// <summary>
  21.         /// Long running from the beginning
  22.         /// </summary>
  23.         static void SlowClient1_OnId2()
  24.         {
  25.             Thread.Sleep(500); // allow a fast thread to start ...
  26.  
  27.             ConcurrencyEmergencyManager.RequestAndWaitForEmergencyGrant(2);
  28.             Console.WriteLine("{0} Slow client works on id 2", DateTime.Now);
  29.             Thread.Sleep(15000);
  30.             Console.WriteLine("{0} Slow client finsihed on id 2", DateTime.Now);
  31.             ConcurrencyEmergencyManager.ReleaseGrantForEmergency(2);
  32.         }
  33.  
  34.         static void FastClient1_OnId1()
  35.         {
  36.             for (int i = 0; i < 4; i++)
  37.             {
  38.                 ConcurrencyEmergencyManager.RequestAndWaitForEmergencyGrant(1);
  39.                 Console.WriteLine("{0} Client 1 works on id 1", DateTime.Now);
  40.                 Thread.Sleep(2000);
  41.                 ConcurrencyEmergencyManager.ReleaseGrantForEmergency(1);
  42.             }
  43.         }
  44.  
  45.         static void FastClient2_OnId1()
  46.         {
  47.             for (int i = 0; i < 4; i++)
  48.             {
  49.                 ConcurrencyEmergencyManager.RequestAndWaitForEmergencyGrant(1);
  50.                 Console.WriteLine("{0} Client 2 works on id 1", DateTime.Now);
  51.                 Thread.Sleep(2500);
  52.                 ConcurrencyEmergencyManager.ReleaseGrantForEmergency(1);
  53.             }
  54.         }
  55.     }
  56.  
  57.     public static class ConcurrencyEmergencyManager
  58.     {
  59.         private static object _syncRootGrant = new object();
  60.         private static List<int> _emergenciesCurrentInUseList = new List<int>();
  61.  
  62.         public static void RequestAndWaitForEmergencyGrant(int emergencyId)
  63.         {
  64.             lock (_syncRootGrant)
  65.             {
  66.                 while (_emergenciesCurrentInUseList.Where(i => i == emergencyId).Count() != 0)
  67.                 {
  68.                     Monitor.Wait(_syncRootGrant);
  69.                 }
  70.  
  71.                 _emergenciesCurrentInUseList.Add(emergencyId);
  72.             }
  73.         }
  74.  
  75.         public static void ReleaseGrantForEmergency(int emergencyId)
  76.         {
  77.             lock (_syncRootGrant)
  78.             {
  79.                 _emergenciesCurrentInUseList.Remove(emergencyId);
  80.  
  81.                 Monitor.PulseAll(_syncRootGrant);
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement