Martina312

Концерт2

Apr 23rd, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 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/g3
  3. package ispiti.concert;
  4.  
  5.  
  6. import ispiti.ProblemExecution;
  7. import ispiti.TemplateThread;
  8.  
  9. import java.util.HashSet;
  10. import java.util.concurrent.Semaphore;
  11.  
  12. public class Concert2 {
  13.  
  14.     static Semaphore performerHere;
  15.     static Semaphore perfomer;
  16.     static Semaphore baritoni;
  17.     static Semaphore tenori;
  18.     static Semaphore tenorHere;
  19.     static Semaphore readyToForm;
  20.     static Semaphore groups;
  21.     static Semaphore formingDone;
  22.     static Semaphore canPerform;
  23.     static Semaphore donePerforming;
  24.  
  25.     public static void init() {
  26.         perfomer = new Semaphore(1);
  27.         performerHere = new Semaphore(0);
  28.         baritoni = new Semaphore(3);
  29.         tenori = new Semaphore(3);
  30.         tenorHere = new Semaphore(0);
  31.         readyToForm = new Semaphore(0);
  32.         groups = new Semaphore(0);
  33.         formingDone = new Semaphore(0);
  34.         canPerform = new Semaphore(0);
  35.         donePerforming = new Semaphore(0);
  36.  
  37.     }
  38.  
  39.     public static class Performer extends TemplateThread {
  40.  
  41.         public Performer(int numRuns) {
  42.             super(numRuns);
  43.         }
  44.  
  45.         @Override
  46.         public void execute() throws InterruptedException {
  47.             perfomer.acquire();
  48.             state.performerHere();
  49.             performerHere.release(6);
  50.  
  51.             groups.acquire(3);
  52.             canPerform.release(6);
  53.             state.perform();
  54.             donePerforming.acquire(6);
  55.  
  56.             state.vote();
  57.             perfomer.release();
  58.             tenori.release(3);
  59.             baritoni.release(3);
  60.         }
  61.  
  62.     }
  63.  
  64.     public static class Baritone extends TemplateThread {
  65.  
  66.         public Baritone(int numRuns) {
  67.             super(numRuns);
  68.         }
  69.  
  70.         @Override
  71.         public void execute() throws InterruptedException {
  72.             baritoni.acquire(); //maks 3 baritoni smeat
  73.             performerHere.acquire(); //mora prvo da se osigurame deka navstina dosol performer
  74.  
  75.             tenorHere.acquire(); //cekame da vidime dali stignal tenor
  76.             readyToForm.release(); //ako stignal znaci moze da se formira grupa
  77.             state.formBackingVocals(); //se formira grupata
  78.             formingDone.acquire(); //dobivame poraki od tenorite, dali se formirale grupite, i taka mu kazuvame na izveduvacot
  79.  
  80.             groups.release();
  81.             canPerform.acquire();
  82.             state.perform();
  83.             donePerforming.release();
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.     public static class Tenor extends TemplateThread {
  90.  
  91.         public Tenor(int numRuns) {
  92.             super(numRuns);
  93.         }
  94.  
  95.         @Override
  96.         public void execute() throws InterruptedException {
  97.             tenori.acquire();
  98.             performerHere.acquire();
  99.  
  100.             tenorHere.release();
  101.             readyToForm.acquire();
  102.             state.formBackingVocals();
  103.             formingDone.release();
  104.  
  105.             canPerform.acquire();
  106.             state.perform();
  107.             donePerforming.release();
  108.         }
  109.  
  110.     }
  111.  
  112.     static Concert2State state = new Concert2State();
  113.  
  114.     public static void main(String[] args) {
  115.         for (int i = 0; i < 10; i++) {
  116.             run();
  117.         }
  118.     }
  119.  
  120.     public static void run() {
  121.         try {
  122.             int numRuns = 1;
  123.             int numScenarios = 300;
  124.  
  125.             HashSet<Thread> threads = new HashSet<Thread>();
  126.  
  127.             for (int i = 0; i < numScenarios; i++) {
  128.                 Tenor t = new Tenor(numRuns);
  129.                 Baritone b = new Baritone(numRuns);
  130.                 threads.add(t);
  131.                 if (i % 3 == 0) {
  132.                     Performer p = new Performer(numRuns);
  133.                     threads.add(p);
  134.                 }
  135.                 threads.add(b);
  136.             }
  137.  
  138.             init();
  139.  
  140.             ProblemExecution.start(threads, state);
  141.         } catch (Exception ex) {
  142.             ex.printStackTrace();
  143.         }
  144.     }
  145.  
  146. }
Add Comment
Please, Sign In to add comment