public void SellCookieTo(RegularCustomer customer) { if (_cookies.Count <= _cookiesSold) return; lock (_cookies[_cookiesSold++]) { Console.WriteLine("\t\t\t\t\t" + customer.Name + " received cookie #" + _cookiesSold + " with " + _cookies[(_cookiesSold-1)].Type); } }
static void Main(string[] args) { var bakery = new Bakery(); var bakeryT = new Thread(bakery.Work); bakeryT.Start(); var fred = new RegularCustomer("Fred"); var ted = new RegularCustomer("Ted"); var greg = new RegularCustomer("Greg"); var fredT = new Thread(() => fred.GetCookiesFrom(bakery)); var tedT = new Thread(() => ted.GetCookiesFrom(bakery)); var gregT = new Thread(() => greg.GetCookiesFrom(bakery)); fredT.Start(); tedT.Start(); gregT.Start(); while (true) { if (bakery.InStock) continue; Console.ReadKey(); break; } }