Exemplo n.º 1
0
        static void SuperQueueDemo()
        {
            var queue = new MySuperQueue <int>();

            var threads = new List <Thread>();

            var thread = new Thread(() => GetItem(queue));

            thread.Name = "Get #1";
            threads.Add(thread);

            thread      = new Thread(() => GetItem(queue));
            thread.Name = "Get #2";
            threads.Add(thread);

            thread = new Thread(() =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(3));
                PutItem(queue, 1);
            });
            thread.Name = "Put #1";
            threads.Add(thread);

            thread = new Thread(() =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(8));
                PutItem(queue, 2);
            });
            thread.Name = "Put #2";
            threads.Add(thread);

            thread      = new Thread(() => GetItem(queue));
            thread.Name = "Get #3";
            threads.Add(thread);

            foreach (var t in threads)
            {
                t.Start();
            }

            //Thread.Sleep(TimeSpan.FromSeconds(10));
            queue.Push(3);

            foreach (var t in threads)
            {
                t.Join();
            }
        }
Exemplo n.º 2
0
 static void PutItem(MySuperQueue <int> queue, int item)
 {
     queue.Push(item);
     Console.WriteLine($"Thread \"{Thread.CurrentThread.Name}\": put {item}");
 }
Exemplo n.º 3
0
 static void GetItem(MySuperQueue <int> queue)
 {
     Console.WriteLine($"Thread \"{Thread.CurrentThread.Name}\": got {queue.Pop()}");
 }