Advertisement
kikosiak

Untitled

Dec 9th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.25 KB | None | 0 0
  1. -----------zad 3-------------------------------------
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ProducentKonsument
  10. {
  11.     class Program
  12.     {
  13.        
  14.         static int licznik = 0;
  15.         static readonly int liczbaWatkow = 10;
  16.         static readonly int liczbaIteracji = 10;
  17.         static EventWaitHandle pisanie = new AutoResetEvent(true);
  18.         static EventWaitHandle czytanie = new AutoResetEvent(false);
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             List<Thread> watkiKonsument = new List<Thread>();
  23.             Random rand = new Random(); //random wspólny
  24.             for (int i = 0;i<liczbaWatkow;i++)
  25.             {
  26.                 var konsument = new Thread(() =>
  27.                 {
  28.                   //  Random rand = new Random(); //random lokalny seed oparty na czasie
  29.                     //Random rand = new Random(Thread.CurrentThread.ManagedThreadId); //random lokalny seed oparty na Id wątku
  30.  
  31.                     for (int j = 0; j < liczbaIteracji; j++)
  32.                     {
  33.                      
  34.                         var czasUspienia = rand.Next(1000);
  35.                        // Console.Write($"{czasUspienia} ");
  36.                         Thread.Sleep(czasUspienia);
  37.                        pisanie.WaitOne();
  38.                        Console.Write("{0}",czasUspienia);
  39.                        
  40.                        
  41.                         //Console.WriteLine($"[{licznik}] ");
  42.                        
  43.                         Console.WriteLine("[{0}] ",licznik);
  44.                         czytanie.Set();
  45.  
  46.                        
  47.                     }
  48.                
  49.                  
  50.                 });
  51.                 watkiKonsument.Add(konsument);
  52.                 konsument.Start();
  53.                
  54.             }
  55.  
  56.             Thread watekProducent = new Thread(() =>
  57.             {
  58.                 Random randPiszacy = new Random(Thread.CurrentThread.ManagedThreadId);
  59.                 try
  60.                 {
  61.                  
  62.                     while (true)
  63.                     {
  64.                     pisanie.Set();
  65.                     czytanie.WaitOne();
  66.                         Thread.Sleep(randPiszacy.Next(100));
  67.                         Interlocked.Increment(ref licznik);
  68.                     }
  69.                 }
  70.                 catch (ThreadInterruptedException)
  71.                 {
  72.                     Console.WriteLine("\nWątek piszący został zakończony");
  73.                 }
  74.          
  75.                
  76.             });
  77.  
  78.             watekProducent.Start();
  79.  
  80.             foreach (var watek in watkiKonsument)
  81.             {
  82.                 watek.Join();
  83.             }
  84.            
  85.             watekProducent.Interrupt();
  86.             watekProducent.Join();
  87.  
  88.             Console.WriteLine("To już jest koniec, naciśnij ENTER...");
  89.             Console.ReadLine();
  90.         }
  91.     }
  92. }
  93.  
  94. ---------------------zad 4-----------------------------
  95. using System;
  96. using System.Collections.Generic;
  97. using System.Linq;
  98. using System.Text;
  99. using System.Threading;
  100. using System.Threading.Tasks;
  101.  
  102. namespace ProducentKonsument
  103. {
  104.     class Program
  105.     {
  106.  
  107.         static int licznik = 0;
  108.         static readonly int liczbaWatkow = 10;
  109.         static readonly int liczbaIteracji = 10;
  110.  
  111.  
  112.         static EventWaitHandle pisanie = new ManualResetEvent(true);
  113.         static EventWaitHandle czytanie = new ManualResetEvent(false);
  114.  
  115.      
  116.         static Mutex mutex = new Mutex(false, "tu.kielce.pl mutex");
  117.  
  118.  
  119.  
  120.  
  121.         static void Main(string[] args)
  122.         {
  123.             List<Thread> watkiKonsument = new List<Thread>();
  124.             //Random rand = new Random(); //random wspólny
  125.             for (int i = 0; i < liczbaWatkow; i++)
  126.             {
  127.                 var konsument = new Thread(() =>
  128.                 {
  129.                     //Random rand = new Random(); //random lokalny seed oparty na czasie
  130.                     Random rand = new Random(Thread.CurrentThread.ManagedThreadId); //random lokalny seed oparty na Id wątku
  131.  
  132.                     for (int j = 0; j < liczbaIteracji; j++)
  133.                     {
  134.  
  135.                         var czasUspienia = rand.Next(1000);
  136.                         mutex.WaitOne();
  137.  
  138.                         czytanie.WaitOne();
  139.  
  140.                         czytanie.Reset();
  141.  
  142.                         mutex.ReleaseMutex();
  143.                         pisanie.Reset();
  144.                        
  145.                         Console.Write($"{czasUspienia} ");  
  146.                         Console.WriteLine($"[{licznik}] ");
  147.  
  148.                        
  149.                         pisanie.Set();
  150.  
  151.                         Thread.Sleep(czasUspienia);
  152.                        
  153.                     }
  154.                 });
  155.                 watkiKonsument.Add(konsument);
  156.                 konsument.Start();
  157.             }
  158.  
  159.             Thread watekProducent = new Thread(() =>
  160.             {
  161.                 Random randPiszacy = new Random(Thread.CurrentThread.ManagedThreadId);
  162.                 try
  163.                 {
  164.                     while (true)
  165.                     {
  166.                        
  167.                         pisanie.WaitOne();
  168.                         pisanie.Reset();
  169.  
  170.                      
  171.                         Interlocked.Increment(ref licznik);
  172.                    
  173.  
  174.                         czytanie.Set();
  175.                        
  176.  
  177.  
  178.                         Thread.Sleep(randPiszacy.Next(100));
  179.                     }
  180.                 }
  181.                 catch (ThreadInterruptedException)
  182.                 {
  183.                     Console.WriteLine("\nWątek piszący został zakończony");
  184.                 }
  185.             });
  186.  
  187.             watekProducent.Start();
  188.  
  189.             foreach (var watek in watkiKonsument)
  190.             {
  191.                 watek.Join();
  192.             }
  193.  
  194.             watekProducent.Interrupt();
  195.             watekProducent.Join();
  196.  
  197.             Console.WriteLine("To już jest koniec, naciśnij ENTER...");
  198.             Console.ReadLine();
  199.         }
  200.     }
  201. }
  202.  
  203. -------------------zad 6-----------------------------
  204. using System;
  205. using System.Collections.Generic;
  206. using System.Threading;
  207.  
  208. namespace SynchroWait
  209. {
  210.     class Program
  211.     {
  212.  
  213.         static List<Thread> watki = new List<Thread>();
  214.         static readonly int liczbaIteracji = 5;
  215.         static readonly int liczbaWatkow = 10;
  216.         static Random rand = new Random();
  217.  
  218.         private static readonly EventWaitHandle[] wh1 = new EventWaitHandle[liczbaWatkow];
  219.         private static readonly EventWaitHandle[] wh2 = new EventWaitHandle[liczbaIteracji];
  220.  
  221.  
  222.         static void Main(string[] args)
  223.         {
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.             for (int i = 0; i < liczbaWatkow; i++)
  232.             {
  233.                 var watek = new Thread((o) =>
  234.                 {
  235.                     int nrWatku = (int)o;
  236.                     Thread.Sleep(rand.Next(1000));
  237.  
  238.                     Console.WriteLine($"Wątek nr {nrWatku} wystartował");
  239.                     for (int j = 0; j < liczbaIteracji; j++)
  240.                     {
  241.                        wh1[liczbaIteracji] = new AutoResetEvent(false);
  242.                         Console.WriteLine($"Wątek nr {nrWatku} pracuje w iteracji nr {j}");
  243.                         Thread.Sleep(rand.Next(1000));
  244.                         WaitHandle.WaitAll(wh1);
  245.  
  246.  
  247.                  
  248.                     }
  249.                     Thread.Sleep(rand.Next(1000));
  250.                    
  251.  
  252.  
  253.  
  254.                     Console.WriteLine($"Wątek nr {nrWatku} zakończył działanie");
  255.                 });
  256.                 watki.Add(watek);
  257.                 watek.Start(i);
  258.             }
  259.  
  260.             Console.WriteLine("---------------- Doczekaliśmy się -----------------");
  261.  
  262.             foreach (var w in watki)
  263.             {
  264.                 w.Join();
  265.             }
  266.  
  267.             Console.WriteLine("To już jest koniec, naciśnij ENTER...");
  268.             Console.ReadLine();
  269.         }
  270.     }
  271. }
  272.  
  273. ---------------------zad 7----------------------------
  274. using System;
  275. using System.Collections.Generic;
  276. using System.Diagnostics;
  277. using System.Threading;
  278.  
  279. namespace Muteksy
  280. {
  281.     class Program
  282.     {
  283.         static List<Thread> watki = new List<Thread>();
  284.  
  285.         static readonly int liczbaIteracji = 10000;
  286.         static readonly int liczbaWatkow = 100;
  287.        
  288.         static int licznik = 0;
  289.        
  290.         static void Main(string[] args)
  291.         {
  292.             #region Bez synchronizacji
  293.  
  294.             for (int i = 0; i < liczbaWatkow; i++)
  295.             {
  296.                 var watek = new Thread((o) =>
  297.                 {
  298.                     int nrWatku = (int)o;
  299.                    
  300.                     for (int j = 0; j < liczbaIteracji; j++)
  301.                     {
  302.                             licznik++;
  303.                     }
  304.  
  305.                 });
  306.                 watki.Add(watek);
  307.                 watek.Start(i);
  308.             }
  309.            
  310.             foreach (var w in watki)
  311.             {
  312.                 w.Join();
  313.             }
  314.            
  315.             Console.WriteLine($"Licznik: {licznik}");
  316.             #endregion
  317.  
  318.            
  319.  
  320.             Console.WriteLine("To już jest koniec, naciśnij ENTER...");
  321.             Console.ReadLine();
  322.         }
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement