changed 3 years ago
Linked with GitHub


建立兩條執行續,每一條都對count+1=>執行1萬次,當每條執行續執行完成後,預計Count結果會是2萬

因此我們可以透過一些管控機制來使得Count會是預期的結果2萬次,這樣的行為就可以稱為同步化

SampleCode:

public static int count = 0; void Main() { var t1 =new Thread(() => {AddCount();}); var t2 =new Thread(() => {AddCount();}); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Console.WriteLine($"最終Count 數字為 = {count}"); } public void AddCount() { for (int i = 0; i < 10000; i++) { count++; } }
Select a repo