Advertisement
Guest User

bemmater

a guest
May 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace THREADING_4___ZADATAK_3_RADNIKA
  9. {
  10.     class Program
  11.     {
  12.         static AutoResetEvent a = new AutoResetEvent(false);
  13.         static AutoResetEvent a2 = new AutoResetEvent(true);
  14.         static CountdownEvent count = new CountdownEvent(2);
  15.  
  16.        
  17.         static readonly object obj = new object();
  18.         static bool pridruzivanje = false;
  19.         static int brojDzakova = 50;
  20.  
  21.         static int plata1 = 0;
  22.         static int plata2 = 0;
  23.         static int plata3 = 0;
  24.  
  25.         static void izbaciDzak()
  26.         {
  27.             lock(obj)
  28.             {
  29.                 brojDzakova--;
  30.             }
  31.         }
  32.  
  33.         static void Plata()
  34.         {
  35.             string ime = Thread.CurrentThread.Name;
  36.             if (ime == "radnik1")
  37.             {
  38.                 count.Wait();
  39.                 Console.WriteLine(".......KRAJ RADNOG DANA..........");
  40.                 Console.WriteLine("Plata prvog radnika: {0}\nPlata drugog radnika: {1}\nPlata treceg radnika: {2}\n", plata1, plata2, plata3);
  41.  
  42.  
  43.             }
  44.             else if (ime == "radnik2")
  45.             {
  46.                 count.Signal();
  47.             }
  48.             else if (ime == "radnik3")
  49.             {
  50.                 count.Signal();
  51.             }
  52.         }
  53.  
  54.         static void Funkcija()
  55.         {
  56.             string ime = Thread.CurrentThread.Name;
  57.  
  58.             while (brojDzakova > 0)
  59.             {
  60.                 if (ime == "radnik1")
  61.                 {
  62.                     a.Set();
  63.                     Console.WriteLine(ime + " je izbacio dzak.");
  64.                     Thread.Sleep(1000);
  65.                     plata1 += 1000;
  66.                     izbaciDzak();
  67.  
  68.                 }
  69.                 if (ime == "radnik2")
  70.                 {
  71.                     a.WaitOne();
  72.                     a2.Set();
  73.                     Console.WriteLine(ime + " priprema materijal.");
  74.                     Thread.Sleep(500);
  75.                     plata2 += 500;
  76.                  
  77.                 }
  78.                 if(ime == "radnik3")
  79.                 {
  80.                     if(pridruzivanje == false)
  81.                     {
  82.                         Thread.Sleep(10000);
  83.                         pridruzivanje = true;
  84.                         Console.WriteLine(ime + " se pridruzuje ostalim radnicima.");
  85.                         plata3 += 10000;
  86.                     }
  87.                     else
  88.                     {
  89.                         Thread.Sleep(1500);
  90.                         Console.WriteLine(ime + " pomaze ostalim radnicima.");
  91.                         izbaciDzak();
  92.                         plata3 += 1500;
  93.                     }
  94.                 }
  95.  
  96.             }
  97.             Plata();
  98.         }
  99.  
  100.        
  101.  
  102.         static void Main(string[] args)
  103.         {
  104.             Thread t = new Thread(Funkcija);
  105.             t.Name = "radnik1";
  106.             t.Start();
  107.  
  108.             Thread t1 = new Thread(Funkcija);
  109.             t1.Name = "radnik2";
  110.             t1.Start();
  111.  
  112.             Thread t2 = new Thread(Funkcija);
  113.             t2.Name = "radnik3";
  114.             t2.Start();
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement