Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ProducentKonsument
- {
- class Program
- {
- static int licznik = 0;
- static readonly int liczbaWatkow = 10;
- static readonly int liczbaIteracji = 10;
- static EventWaitHandle pisanie = new AutoResetEvent(true);
- static EventWaitHandle czytanie = new AutoResetEvent(false);
- static void Main(string[] args)
- {
- List<Thread> watkiKonsument = new List<Thread>();
- for (int i = 0; i < liczbaWatkow; i++)
- {
- var konsument = new Thread(() =>
- {
- Random rand = new Random(Thread.CurrentThread.ManagedThreadId); //random lokalny seed oparty na Id wątku
- for (int j = 0; j < liczbaIteracji; j++)
- {
- var czasUspienia = rand.Next(1000);
- czytanie.WaitOne();
- Console.Write("{0}", czasUspienia);
- Console.WriteLine("[{0}] ", licznik);
- pisanie.Set();
- Thread.Sleep(czasUspienia);
- }
- });
- watkiKonsument.Add(konsument);
- konsument.Start();
- }
- Thread watekProducent = new Thread(() =>
- {
- Random randPiszacy = new Random(Thread.CurrentThread.ManagedThreadId);
- try
- {
- while (true)
- {
- pisanie.WaitOne();
- Thread.Sleep(randPiszacy.Next(100));
- Interlocked.Increment(ref licznik);
- czytanie.Set();
- }
- }
- catch (ThreadInterruptedException)
- {
- Console.WriteLine("\nWątek piszący został zakończony");
- }
- });
- watekProducent.Start();
- foreach (var watek in watkiKonsument)
- {
- watek.Join();
- }
- watekProducent.Interrupt();
- watekProducent.Join();
- Console.WriteLine("To już jest koniec, naciśnij ENTER...");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement