Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace EnterTheDoor
- {
- class StartUp
- {
- static readonly object syncObj = new object();
- static void Main(string[] args)
- {
- List<Thread> threads = new List<Thread>()
- {
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); }),
- new Thread(() => { EnterTheDoor(); })
- };
- threads.ForEach(t => t.Start());
- }
- private static void EnterTheDoor()
- {
- lock (syncObj)
- {
- Console.WriteLine($"Person {Thread.CurrentThread.ManagedThreadId} Passes through the Door");
- Thread.Sleep(2000);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment