public void Consume(long valuesCount) { var rand = new Random(_rank); int consumedCount = 0; while (valuesCount > consumedCount) { //Communicating // Console.WriteLine(string.Format($"Consumer #{_rank} begun communication")); _buffer.Communicate(rand.Next(200, 100000)); //Console.WriteLine(string.Format($"Consumer #{_rank} begun obtaining value from buffer")); int consumedVal = _buffer.Get(); //Console.WriteLine(string.Format($"Consumer #{_rank} obtained value [{consumedVal}]")); //Consuming Thread.Sleep(rand.Next(200, 1000)); consumedCount++; //Console.WriteLine(string.Format($"Consumer #{_rank} consumed value [{consumedVal}]")); } //Console.WriteLine(string.Format($"Consumer #{_rank} begun POST WORK communication")); _buffer.Communicate(50000000); Console.WriteLine(string.Format($"Consumer #{_rank} finished POST WORK communication")); }
public void Produce(long valuesCount) { Random rand = new Random(_rank); int producedCount = 0; while (valuesCount > producedCount) { //Communicating //Console.WriteLine(string.Format($"Producer #{_rank} begun communication")); _buffer.Communicate(rand.Next(200, 100000)); //Producing //Console.WriteLine(string.Format($"Producer #{_rank} begun producing")); Thread.Sleep(rand.Next(200, 1000)); int newVal = rand.Next(); //Console.WriteLine(string.Format($"Producer #{_rank} produced new value [{newVal}]")); _buffer.Put(newVal); producedCount++; //Console.WriteLine(string.Format($"Producer #{_rank} put new value [{newVal}] in buffer")); } Console.WriteLine(string.Format($"Producer #{_rank} begun POST WORK communication")); _buffer.Communicate(50000000); Console.WriteLine(string.Format($"Producer #{_rank} finished POST WORK communication")); }