예제 #1
0
        // sorts the bottle of bottlesMade queue and adds it to either bottleSortedSoda or BottleSortedBeer depending on what type of Id the bottle has
        public static void SortProducedBottles()
        {
            while (true)
            {
                lock (bottlesMade)
                {
                    while (bottlesMade.Count == 0)
                    {
                        Monitor.Wait(bottlesMade);
                        Console.WriteLine("waiting for bottle");
                    }

                    Bottle temp = bottlesMade.Dequeue();

                    if (temp.Id.Substring(0, 2) == "10")
                    {
                        lock (bottlesSortedSoda)
                        {
                            bottlesSortedSoda.Enqueue(temp);
                            Console.WriteLine(
                                $"id : {temp.Id}  type :{temp.Name} has been added to Soda queue");

                            Monitor.PulseAll(bottlesSortedSoda);
                        }
                    }

                    else if (temp.Id.Substring(0, 2) == "20")
                    {
                        lock (bottlesSortedBeer)
                        {
                            bottlesSortedBeer.Enqueue(temp);
                            Console.WriteLine(
                                $"id : {temp.Id}  type :{temp.Name} has been added to Beer queue");

                            Monitor.PulseAll(bottlesSortedBeer);
                        }
                    }
                }

                Thread.Sleep(rand.Next(1000));
            }
        }
예제 #2
0
        // Dequeue a beer from bottlesSortedBeer
        public static void ConsumeBeer()
        {
            while (true)
            {
                lock (bottlesSortedBeer)
                {
                    while (bottlesSortedBeer.Count == 0)
                    {
                        Console.WriteLine("waiting for beer");
                        Monitor.Wait(bottlesSortedBeer);
                    }

                    Bottle temp = bottlesSortedBeer.Dequeue();

                    Console.WriteLine($"id : {temp.Id}  type :{temp.Name} has been consumed");
                }

                Thread.Sleep(rand.Next(1000));
            }
        }
예제 #3
0
        // makes either a soda bottle or beer bottle and enqueue it into bottlesMade queue
        public static void ProduceBottles()
        {
            while (true)
            {
                lock (bottlesMade)
                {
                    while (bottlesMade.Count != 1)

                    {
                        Bottle temp = new Bottle();

                        int num = rand.Next(0, 2);

                        if (num == 1)
                        {
                            counter++;
                            temp = new Bottle()
                            {
                                Id = SetIdSoda(counter), Name = "Soda"
                            };
                            Console.WriteLine($"id : {temp.Id}  type :{temp.Name} has been produced");
                        }
                        else
                        {
                            counter++;
                            temp = new Bottle()
                            {
                                Id = SetIdBeer(counter), Name = "Beer"
                            };
                            Console.WriteLine($"id : {temp.Id}  type :{temp.Name} has been produced");
                        }

                        bottlesMade.Enqueue(temp);
                    }

                    Monitor.PulseAll(bottlesMade);
                }

                Thread.Sleep(rand.Next(500));
            }
        }