ZivkicaI

Filip Gym2

Mar 26th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package mk.ukim.finki.os.synchronization.exam14.june;
  2.  
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6. import java.util.concurrent.Semaphore;
  7.  
  8. import mk.ukim.finki.os.synchronization.ProblemExecution;
  9. import mk.ukim.finki.os.synchronization.TemplateThread;
  10.  
  11. public class Gym2Synchronization {
  12.     public static Semaphore sala;
  13.     public static Semaphore presoblekuvalna;
  14.     public static Semaphore spremni;
  15.     public static Semaphore gotovi;
  16.     static int brojIgraci;
  17.     static int brSobleceni;
  18.  
  19.     public static void init() {
  20.         sala=new Semaphore(12);
  21.         presoblekuvalna=new Semaphore(4);
  22.         spremni=new Semaphore(0);
  23.         gotovi=new Semaphore(0);
  24.         brojIgraci=0;
  25.         brSobleceni=0;
  26.     }
  27.    
  28.  
  29.     public static class Player extends TemplateThread {
  30.  
  31.         public Player(int numRuns) {
  32.             super(numRuns);
  33.         }
  34.  
  35.         @Override
  36.         public void execute() throws InterruptedException {
  37.             sala.acquire();
  38.             state.vlezi();
  39.             synchronized(state)
  40.             {
  41.                 brojIgraci++;
  42.                  if(brojIgraci==12)
  43.                  {
  44.                     spremni.release(12);
  45.                  }
  46.             }
  47.             spremni.acquire();//tuka gi blokiram ako ne se 12, odnosno tuka zastanuvaat site
  48.             state.sportuvaj();//tuka koga kje dojdat 12 gi pushtam naednash site da kazat state.sportuvaj!!!
  49.             presoblekuvalna.acquire(); // Toa e tuka sto ti kazhav za deka ne mora da se 4ca no mora da se povejke od 2
  50.             state.presobleci();
  51.             presoblekuvalna.release();
  52.             synchronized(state)
  53.             {
  54.                 brojIgraci--;
  55.                
  56.                 if(brojIgraci==0)
  57.                 {
  58.                     state.slobodnaSala();
  59.                     sala.release(12);
  60.                    
  61.                 }
  62.             }
  63.            
  64.  
  65.            
  66.         }
  67.     }
  68.     static Gym2State state = new Gym2State();
  69.  
  70.     public static void main(String[] args) {
  71.         for (int i = 0; i < 10; i++) {
  72.             run();
  73.         }
  74.     }
  75.  
  76.     public static void run() {
  77.         try {
  78.             Scanner s = new Scanner(System.in);
  79.             int numRuns = 1;
  80.             int numIterations = 1200;
  81.             s.close();
  82.  
  83.             HashSet<Thread> threads = new HashSet<Thread>();
  84.  
  85.             for (int i = 0; i < numIterations; i++) {
  86.                 Player h = new Player(numRuns);
  87.                 threads.add(h);
  88.             }
  89.  
  90.             init();
  91.  
  92.             ProblemExecution.start(threads, state);
  93.             System.out.println(new Date().getTime());
  94.         } catch (Exception ex) {
  95.             ex.printStackTrace();
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment