using System; using System.Threading; namespace huangyibiao { public class BookShop { //共享资源 public int _iBookNumber = 1; public void Sale() { int iTemp; lock (this) { iTemp = _iBookNumber; if (iTemp > 0) { //卖书过程: 1秒 Thread.Sleep(1000); _iBookNumber -= 1; Console.WriteLine("售出一本书!"); } else { Console.WriteLine("已经没有书了!"); } } } } class Program { public static void Main() { BookShop bs = new BookShop(); Thread thr1 = new Thread(new ThreadStart(bs.Sale)); Thread thr2 = new Thread(new ThreadStart(bs.Sale)); thr1.Start(); thr2.Start(); Console.Read(); } } }