Skip to content

Instantly share code, notes, and snippets.

@huanlin
Created May 5, 2013 17:00
Show Gist options
  • Save huanlin/5521396 to your computer and use it in GitHub Desktop.
Save huanlin/5521396 to your computer and use it in GitHub Desktop.
public class SharedStateDemo
{
private int itemCount = 0;
private object locker = new Object(); // 用於獨佔鎖定的物件
public void Run()
{
var t1 = new Thread(AddToCart);
var t2 = new Thread(AddToCart);
t1.Start(300);
t2.Start(100);
}
private void AddToCart(object simulateDelay)
{
Console.WriteLine("Enter thread {0}", // 顯示目前所在的執行緒編號
Thread.CurrentThread.ManagedThreadId);
lock (locker) // 利用 locker 物件來鎖定程式區塊
{
itemCount++;
Thread.Sleep((int)simulateDelay);
Console.WriteLine("Items in cart: {0} on thread {1}",
itemCount, Thread.CurrentThread.ManagedThreadId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment