WindFell

Enter The Door Threads

Apr 5th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace EnterTheDoor
  6. {
  7.     class StartUp
  8.     {
  9.         static readonly object syncObj = new object();
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             List<Thread> threads = new List<Thread>()
  14.             {
  15.                 new Thread(() => { EnterTheDoor(); }),
  16.                 new Thread(() => { EnterTheDoor(); }),
  17.                 new Thread(() => { EnterTheDoor(); }),
  18.                 new Thread(() => { EnterTheDoor(); }),
  19.                 new Thread(() => { EnterTheDoor(); }),
  20.                 new Thread(() => { EnterTheDoor(); }),
  21.                 new Thread(() => { EnterTheDoor(); }),
  22.                 new Thread(() => { EnterTheDoor(); }),
  23.                 new Thread(() => { EnterTheDoor(); }),
  24.                 new Thread(() => { EnterTheDoor(); })
  25.             };
  26.  
  27.             threads.ForEach(t => t.Start());
  28.         }
  29.  
  30.         private static void EnterTheDoor()
  31.         {
  32.             lock (syncObj)
  33.             {
  34.                 Console.WriteLine($"Person {Thread.CurrentThread.ManagedThreadId} Passes through the Door");
  35.                 Thread.Sleep(2000);
  36.             }
  37.         }
  38.     }
  39. }
Add Comment
Please, Sign In to add comment