Latkoski

Сала

Mar 23rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 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 vlez_site;
  13.     public static Semaphore sportuvaj;
  14.     public static Semaphore sobleci_se;
  15.     public static Semaphore kabina_eden;
  16.     public static Semaphore mutex;
  17.     public static int brojac = 0;
  18.     public static void init() {
  19.         vlez_site = new Semaphore(12);
  20.         sportuvaj = new Semaphore(0);
  21.         sobleci_se = new Semaphore(4);
  22.         mutex = new Semaphore(1);
  23.        
  24.     }
  25.    
  26.  
  27.     public static class Player extends TemplateThread {
  28.  
  29.         public Player(int numRuns) {
  30.             super(numRuns);
  31.         }
  32.  
  33.         @Override
  34.         public void execute() throws InterruptedException {
  35.             vlez_site.acquire();
  36.             state.vlezi();
  37.             mutex.acquire();
  38.             brojac++;
  39.             if(brojac==12)
  40.                 sportuvaj.release(12);
  41.             mutex.release();
  42.            
  43.             sportuvaj.acquire();
  44.             state.sportuvaj();
  45.            
  46.             sobleci_se.acquire();
  47.             state.presobleci();
  48.             sobleci_se.release();
  49.            
  50.             mutex.acquire();
  51.             brojac--;
  52.             if(brojac==0){
  53.                 state.slobodnaSala();
  54.                 vlez_site.release(12);
  55.             }
  56.             mutex.release();
  57.         }
  58.     }
  59.     static Gym2State state = new Gym2State();
  60.  
  61.     public static void main(String[] args) {
  62.         for (int i = 0; i < 10; i++) {
  63.             run();
  64.         }
  65.     }
  66.  
  67.     public static void run() {
  68.         try {
  69.             Scanner s = new Scanner(System.in);
  70.             int numRuns = 1;
  71.             int numIterations = 1200;
  72.             s.close();
  73.  
  74.             HashSet<Thread> threads = new HashSet<Thread>();
  75.  
  76.             for (int i = 0; i < numIterations; i++) {
  77.                 Player h = new Player(numRuns);
  78.                 threads.add(h);
  79.             }
  80.  
  81.             init();
  82.  
  83.             ProblemExecution.start(threads, state);
  84.             System.out.println(new Date().getTime());
  85.         } catch (Exception ex) {
  86.             ex.printStackTrace();
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment