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.Tasks;
- using System.Threading;
- namespace THREADING_4___ZADATAK_3_RADNIKA
- {
- class Program
- {
- static AutoResetEvent a = new AutoResetEvent(false);
- static AutoResetEvent a2 = new AutoResetEvent(true);
- static CountdownEvent count = new CountdownEvent(2);
- static readonly object obj = new object();
- static bool pridruzivanje = false;
- static int brojDzakova = 50;
- static int plata1 = 0;
- static int plata2 = 0;
- static int plata3 = 0;
- static void izbaciDzak()
- {
- lock(obj)
- {
- brojDzakova--;
- }
- }
- static void Plata()
- {
- string ime = Thread.CurrentThread.Name;
- if (ime == "radnik1")
- {
- count.Wait();
- Console.WriteLine(".......KRAJ RADNOG DANA..........");
- Console.WriteLine("Plata prvog radnika: {0}\nPlata drugog radnika: {1}\nPlata treceg radnika: {2}\n", plata1, plata2, plata3);
- }
- else if (ime == "radnik2")
- {
- count.Signal();
- }
- else if (ime == "radnik3")
- {
- count.Signal();
- }
- }
- static void Funkcija()
- {
- string ime = Thread.CurrentThread.Name;
- while (brojDzakova > 0)
- {
- if (ime == "radnik1")
- {
- a.Set();
- Console.WriteLine(ime + " je izbacio dzak.");
- Thread.Sleep(1000);
- plata1 += 1000;
- izbaciDzak();
- }
- if (ime == "radnik2")
- {
- a.WaitOne();
- a2.Set();
- Console.WriteLine(ime + " priprema materijal.");
- Thread.Sleep(500);
- plata2 += 500;
- }
- if(ime == "radnik3")
- {
- if(pridruzivanje == false)
- {
- Thread.Sleep(10000);
- pridruzivanje = true;
- Console.WriteLine(ime + " se pridruzuje ostalim radnicima.");
- plata3 += 10000;
- }
- else
- {
- Thread.Sleep(1500);
- Console.WriteLine(ime + " pomaze ostalim radnicima.");
- izbaciDzak();
- plata3 += 1500;
- }
- }
- }
- Plata();
- }
- static void Main(string[] args)
- {
- Thread t = new Thread(Funkcija);
- t.Name = "radnik1";
- t.Start();
- Thread t1 = new Thread(Funkcija);
- t1.Name = "radnik2";
- t1.Start();
- Thread t2 = new Thread(Funkcija);
- t2.Name = "radnik3";
- t2.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement