Advertisement
kikosiak

Untitled

Dec 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ProducentKonsument
  9. {
  10.     class Program
  11.     {
  12.         static int licznik = 0;
  13.         static readonly int liczbaWatkow = 10;
  14.         static readonly int liczbaIteracji = 10;
  15.         static EventWaitHandle pisanie = new AutoResetEvent(true);
  16.         static EventWaitHandle czytanie = new AutoResetEvent(false);
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             List<Thread> watkiKonsument = new List<Thread>();
  21.             for (int i = 0; i < liczbaWatkow; i++)
  22.             {
  23.                 var konsument = new Thread(() =>
  24.                 {
  25.                     Random rand = new Random(Thread.CurrentThread.ManagedThreadId); //random lokalny seed oparty na Id wątku
  26.  
  27.                     for (int j = 0; j < liczbaIteracji; j++)
  28.                     {
  29.                         var czasUspienia = rand.Next(1000);
  30.                        
  31.                         czytanie.WaitOne();
  32.                         Console.Write("{0}", czasUspienia);
  33.  
  34.                         Console.WriteLine("[{0}] ", licznik);
  35.                         pisanie.Set();
  36.                         Thread.Sleep(czasUspienia);
  37.                     }
  38.                 });
  39.                 watkiKonsument.Add(konsument);
  40.                 konsument.Start();
  41.             }
  42.  
  43.             Thread watekProducent = new Thread(() =>
  44.             {
  45.                 Random randPiszacy = new Random(Thread.CurrentThread.ManagedThreadId);
  46.                 try
  47.                 {
  48.                     while (true)
  49.                     {
  50.                         pisanie.WaitOne();                    
  51.                         Thread.Sleep(randPiszacy.Next(100));
  52.                         Interlocked.Increment(ref licznik);
  53.                         czytanie.Set();
  54.                     }
  55.                 }
  56.                 catch (ThreadInterruptedException)
  57.                 {
  58.                     Console.WriteLine("\nWątek piszący został zakończony");
  59.                 }
  60.             });
  61.  
  62.             watekProducent.Start();
  63.  
  64.             foreach (var watek in watkiKonsument)
  65.             {
  66.                 watek.Join();
  67.             }
  68.  
  69.             watekProducent.Interrupt();
  70.             watekProducent.Join();
  71.  
  72.             Console.WriteLine("To już jest koniec, naciśnij ENTER...");
  73.             Console.ReadLine();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement