Untitled

 avatar
unknown
plain_text
2 years ago
1.0 kB
3
Indexable
using System;
using System.Collections.Generic;
using System.Threading;

class Program
{
    static Queue<int> queue = new Queue<int>();
    static void Main(string[] args)
    {
        Thread t1 = new Thread(() =>
        {
            Random r = new Random();
            while (true)
            {
                int n = r.Next();
                lock (queue)
                {
                    queue.Enqueue(n);
                }
                Thread.Sleep(1000);
            }
        });
        Thread t2 = new Thread(() =>
        {
            int sum = 0;
            while (true)
            {
                lock (queue)
                {
                    while (queue.Count > 0)
                    {
                        sum += queue.Dequeue();
                    }
                }
                Console.WriteLine("Sum: " + sum);
                Thread.Sleep(5000);
            }
        });
        t1.Start();
        t2.Start();
        Console.ReadKey();
    }
}
Editor is loading...