Martina312

ОС - Музичари

Apr 23rd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. //reshenie na :
  2. //https://github.com/finki-mk/OS/tree/master/synchronization/src/main/java/mk/ukim/finki/os/synchronization/exam17/k1/g1
  3.  
  4. package ispiti;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.concurrent.Semaphore;
  9.  
  10. public class Muzichari {
  11.  
  12.     static Semaphore gitaristi;
  13.     static Semaphore pejaci;
  14.     static Semaphore pejaciLock;
  15.     static Semaphore gitaristHere;
  16.     static Semaphore ready;
  17.     static Semaphore finished;
  18.     static int numPejaci = 0;
  19.  
  20.     static State state = new State();
  21.  
  22.     public static void init(){
  23.         gitaristi = new Semaphore(3);
  24.         pejaci = new Semaphore(2);
  25.         pejaciLock = new Semaphore(1);
  26.         gitaristHere = new Semaphore(0);
  27.         ready = new Semaphore(0);
  28.         finished = new Semaphore(0);
  29.  
  30.     }
  31.  
  32.  
  33.     private static class Pejac extends Thread{
  34.  
  35.         public void execute() throws InterruptedException {
  36.             pejaci.acquire();
  37.             pejaciLock.acquire();
  38.             numPejaci++;
  39.             if(numPejaci == 2){ //ovoj ke bide kontroler
  40.                 gitaristHere.acquire(3); //bara dali vekje ima stignato 3 gitaristi
  41.                 ready.release(5); //sega e oformen bendot, da se izvestat site
  42.             }
  43.             pejaciLock.release();
  44.  
  45.             ready.acquire();
  46.             state.play("Pejac");
  47.             finished.release();
  48.  
  49.             pejaciLock.acquire();
  50.             numPejaci--;
  51.             if(numPejaci==0){
  52.                 finished.acquire(5);
  53.                 state.evaluate();
  54.                 pejaci.release(2);
  55.                 gitaristi.release(3);
  56.             }
  57.             pejaciLock.release();
  58.  
  59.  
  60.         }
  61.         @Override
  62.         public void run() {
  63.             try {
  64.                 execute();
  65.             } catch (InterruptedException e) {
  66.                 e.printStackTrace();
  67.             }
  68.         }
  69.     }
  70.  
  71.     private static class Gitarist extends Thread{
  72.         public void execute() throws InterruptedException {
  73.             gitaristi.acquire();
  74.             gitaristHere.release(); //kazhuva deka pristignal gitarist
  75.             ready.acquire();
  76.             state.play("Gitarist");
  77.             finished.release();
  78.         }
  79.  
  80.         @Override
  81.         public void run() {
  82.             try {
  83.                 execute();
  84.             } catch (InterruptedException e) {
  85.                 e.printStackTrace();
  86.             }
  87.         }
  88.     }
  89.  
  90.     private static class State{
  91.         public void play(String musician){
  92.             System.out.println(musician+" will play");
  93.         }
  94.  
  95.         public void evaluate(){
  96.             System.out.println("Evaluating...");
  97.         }
  98.     }
  99.  
  100.     public static void main(String[] args) throws InterruptedException {
  101.         init();
  102.         List<Thread> threads = new ArrayList<>();
  103.  
  104.         for(int i=0; i<10;i++){
  105.             Pejac p = new Pejac();
  106.             threads.add(p);
  107.             p = new Pejac();
  108.             threads.add(p);
  109.  
  110.             Gitarist g = new Gitarist();
  111.             threads.add(g);
  112.             g = new Gitarist();
  113.             threads.add(g);
  114.             g=new Gitarist();
  115.             threads.add(g);
  116.         }
  117.  
  118.         for(Thread t:threads){
  119.             t.start();
  120.         }
  121.  
  122.         for(Thread t:threads){
  123.             t.join();
  124.         }
  125.  
  126.         System.out.println("Uspeshna sinhronizacija");
  127.  
  128.     }
  129.  
  130. }
Add Comment
Please, Sign In to add comment