Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
0
Indexable
Never
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();
    }
}