Esempio n. 1
0
        static void Main(string[] args)
        {
            LimitedQueue <int> queue = new LimitedQueue <int>(1);

            Task.Run(() =>
            {
                queue.Enqueue(1);
                Console.WriteLine(1);
            });
            Task.Run(() =>
            {
                queue.Enqueue(2);
                Console.WriteLine(2);
            });
            Task.Run(() =>
            {
                queue.Enqueue(3);
                Console.WriteLine(3);
            });
            Task.Run(() =>
            {
                Thread.Sleep(3000);
                queue.Dequeue();
                Thread.Sleep(3000);
                queue.Dequeue();
            });

            Console.ReadLine();
        }
        public void RemoveFromQueue(LimitedQueue <T> queue)
        {
            Random rnd   = new Random();
            int    count = rnd.Next(1, 5);

            while (count > 0)
            {
                Task.Factory.StartNew(() =>
                {
                    queue.Dequeue();
                }
                                      );
                count--;
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            LimitedQueue<int> queue = new LimitedQueue<int>(5);
            for (int i = 0; i < 20; i++)
            {
                int action = new Random().Next(0, 2);
                if (action == 0)
                {
                    var trd = new Thread((j) =>
                     {
                         try
                         {
                             Console.WriteLine($"{j}: Dequeued {queue.Dequeue()}");
                             Console.WriteLine($"{j}: {queue.Count()} ");
                         }
                         catch (InvalidOperationException ex)
                         {
                             Console.WriteLine($"{j}: queue is empty");
                         }

                     }
                        );
                    trd.Name = i.ToString();
                    trd.Start(i);
                }
                else
                {
                    int value = new Random().Next(0, 200);
                    var trd = new Thread((j) =>
                    {
                        Console.WriteLine($"{j}: Try to enqueue {value}");
                        queue.Enque(value);
                        Console.WriteLine($"{j}: Enqueued {value}");
                        Console.WriteLine($"{j}: {queue.Count()} ");


                    });
                    trd.Name = i.ToString();
                    trd.Start(i);

                }

            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            LimitedQueue <int> myQueue = new LimitedQueue <int>(100);

            {
                for (int i = 0; i < 1000; i++)
                {
                    if (i % 3 == 0)
                    {
                        ThreadPool.QueueUserWorkItem((o) => myQueue.Dequeue(), null);
                    }
                    else
                    {
                        ThreadPool.QueueUserWorkItem((o) => myQueue.Enqueue(new Random().Next()), null);
                    }
                }
                Console.ReadLine();
            }
        }