Home [C# 筆記] Threading - C# Another Thread Synchronization Example
Post
Cancel

[C# 筆記] Threading - C# Another Thread Synchronization Example

C# Another Thread Synchronization Example

繼上一個範例

模擬上wc的情境…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
internal class Program
{
    static object baton = new object(); //為lock建立一個接力棒的變數,用它來協調誰可以有使用權
    static Random random = new Random(); //讓線程共享的數據
    static void Main(string[] args)
    {
        //建立5個執行緒來執行UseRestroomStall()方法
        for (int i = 0; i < 5; i++)
        {
            new Thread(UseRestroomStall).Start();
        }
    }

    static void UseRestroomStall()
    {
        //排隊上wc
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} Trying to obtain the bathroom stall...");

        //使用lock來協調 誰可以有使用權
        lock (baton) 
        {
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} obtained the lock. Doing my business...");
            Thread.Sleep(random.Next(2000)); //讓主執行緒睡,睡的最大隨機數時間為2秒
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} leaving the stall.");
        }
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} is leaving the restaurant.");
    }
}

執行結果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
9 Trying to obtain the bathroom stall...
9 obtained the lock. Doing my business...
11 Trying to obtain the bathroom stall...
12 Trying to obtain the bathroom stall...
10 Trying to obtain the bathroom stall...
13 Trying to obtain the bathroom stall...
9 leaving the stall.
10 obtained the lock. Doing my business...
9 is leaving the restaurant.
10 leaving the stall.
11 obtained the lock. Doing my business...
10 is leaving the restaurant.
11 leaving the stall.
12 obtained the lock. Doing my business...
11 is leaving the restaurant.
12 leaving the stall.
12 is leaving the restaurant.
13 obtained the lock. Doing my business...
13 leaving the stall.
13 is leaving the restaurant.
This post is licensed under CC BY 4.0 by the author.