void SplitLuggage(Luggage luggage) { foreach (var item in terminals) { //Find the terminal with matching terminal if (item == luggage.flightplan.Terminal) { Monitor.Enter(item.IncomingLuggage); item.IncomingLuggage.Enqueue(luggage); Debug.WriteLine($"[{Thread.CurrentThread.Name}] Har sendt en bagage afsted til {item.name}"); Monitor.PulseAll(item.IncomingLuggage); Monitor.Exit(item.IncomingLuggage); } } }
void ProduceLuggage() { while (true) { //lock the splitters incoming luggage Monitor.Enter(splitter); Luggage newLuggage = GenerateLuggage(); splitter.Enqueue(newLuggage); //Add luggage to the counter of lifetime made. LuggageMade++; Debug.WriteLine($"[{Thread.CurrentThread.Name}] [{newLuggage.flightplan.Departure}] [{LuggageMade}] Har sendt baggage til splitter"); //Tell that luggage were made LuggageCreated?.Invoke(this, EventArgs.Empty); //Tell all waiting threads its available and exit thread Monitor.PulseAll(splitter); Monitor.Exit(splitter); //Make some more randomness to make the timing of all counters random int b = 4500; byte roll = RollDice((byte)b); Thread.Sleep(roll); Thread.Sleep(rnd.Next(600, 1200)); } }