Created
October 7, 2022 22:12
-
-
Save musoftware/bb9c83f0cc28013379b3cbfd24a9d514 to your computer and use it in GitHub Desktop.
Sequence Thread Starting and wait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
internal class Program | |
{ | |
private static Semaphore _pool = new Semaphore(initialCount: 1, maximumCount: 1); | |
static void Main(string[] args) | |
{ | |
DoA(); | |
DoB(); | |
} | |
private static void DoA() | |
{ | |
new Thread(() => | |
{ | |
Thread.Sleep(1000); | |
try | |
{ | |
_pool.WaitOne(); | |
Console.WriteLine("A Begin"); | |
Thread.Sleep(1000); | |
Console.WriteLine("A Process"); | |
Thread.Sleep(1000); | |
} | |
finally | |
{ | |
_pool.Release(); | |
Console.WriteLine("A End"); | |
} | |
}).Start(); | |
} | |
private static void DoB() | |
{ | |
new Thread(() => | |
{ | |
Thread.Sleep(1000); | |
try | |
{ | |
_pool.WaitOne(); | |
Console.WriteLine("B Begin"); | |
Thread.Sleep(1000); | |
Console.WriteLine("B Process"); | |
Thread.Sleep(1000); | |
} | |
finally | |
{ | |
_pool.Release(); | |
Console.WriteLine("B End"); | |
} | |
}).Start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment