public void Start() { Task.Run(() => { for (int i = 0; i < 10; i++) { Bottle b = new Bottle(); _sem.WaitOne(); // wait for ready Console.WriteLine(_buf.Peek()); _buf.Insert(b); Console.WriteLine("prod buf:" + _buf.Peek()); Thread.Sleep(25); } } ); Task.Run(() => { for (int i = 0; i < 12; i++) { Console.WriteLine(_buf.Peek()); Bottle b = _buf.Take(); Console.WriteLine("Cons: " + b + " peek " + _buf.Peek()); _sem.Release(); } } ); }
private void GenerateBottles(int number) { Bottle bottle; for (int i = 0; i < number; i++) { bottle = new Bottle(); unwashedBottles.Insert(bottle); TraceBottle(bottle); Thread.Sleep(10 + rnd.Next(10)); } }
/* * Washing Bottles */ private void WashBottle(int nr) { String myName = "Wash" + nr; Trace.TraceInformation($"{myName} is started"); while (true) { Bottle b = unwashedBottles.Take(); Thread.Sleep(5 + rnd.Next(15)); b.State = "washed"; TraceBottle(b); washedBottles.Insert(b); } }
/* * Topping Bottles */ private void TopBottle(int nr) { String myName = "Top" + nr; Trace.TraceInformation($"{myName} is started"); while (true) { Bottle b = filledBottles[nr].Take(); Thread.Sleep(15 + rnd.Next(10)); b.State = "Topped"; TraceBottle(b); toppedBottles.Insert(b); toppedIsReady[nr].Release(); } }
/* * Packing Bottles */ private void BoxingBottle(int nr) { String myName = "Counting" + nr; Trace.TraceInformation($"{myName} is started"); List <Bottle> bottleBox; while (true) { bottleBox = new List <Bottle>(); for (int i = 0; i < 24; i++) { Bottle b = toppedBottles.Take(); Thread.Sleep(5 + rnd.Next(10)); b.State = "Boxing"; bottleBox.Add(b); TraceBottle(b); } boxOfBottles.Insert(bottleBox); } }