Crazy

Синхронизација на играчи во сала

Mar 29th, 2018
142
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.  
  13.     static Semaphore avaiableSlots;
  14.     static Semaphore cabineSlots;
  15.     static int igraci;
  16.     static Semaphore igraciLock;
  17.  
  18.  
  19.  
  20.     public static void init() {
  21.  
  22.         igraciLock = new Semaphore(1);
  23.         igraci=0;
  24.  
  25.         cabineSlots = new Semaphore(4);
  26.  
  27.         avaiableSlots = new Semaphore(12);
  28.  
  29.  
  30.  
  31.  
  32.     }
  33.    
  34.  
  35.     public static class Player extends TemplateThread {
  36.  
  37.         public Player(int numRuns) {
  38.             super(numRuns);
  39.         }
  40.  
  41.         @Override
  42.         public void execute() throws InterruptedException {
  43.  
  44.  
  45.             avaiableSlots.acquire();
  46.             state.vlezi();
  47.             igraciLock.acquire();
  48.             igraci++;
  49.             igraciLock.release();
  50.  
  51.  
  52.             if (igraci==12) {
  53.                 state.sportuvaj();
  54.             }
  55.  
  56.  
  57.             igraciLock.acquire();
  58.             igraci--;
  59.             igraciLock.release();
  60.  
  61.  
  62.             cabineSlots.acquire();
  63.  
  64.  
  65.             state.presobleci();
  66.  
  67.             igraciLock.acquire();
  68.             igraci--;
  69.             igraciLock.release();
  70.  
  71.             cabineSlots.release();
  72.  
  73.  
  74.  
  75.             if (igraci==0)
  76.                 state.slobodnaSala();
  77.             avaiableSlots.release(12);
  78.  
  79.  
  80.  
  81.         }
  82.     }
  83.     static Gym2State state = new Gym2State();
  84.  
  85.     public static void main(String[] args) {
  86.         for (int i = 0; i < 10; i++) {
  87.             run();
  88.         }
  89.     }
  90.  
  91.     public static void run() {
  92.         try {
  93.             Scanner s = new Scanner(System.in);
  94.             int numRuns = 1;
  95.             int numIterations = 1200;
  96.             s.close();
  97.  
  98.             HashSet<Thread> threads = new HashSet<Thread>();
  99.  
  100.             for (int i = 0; i < numIterations; i++) {
  101.                 Player h = new Player(numRuns);
  102.                 threads.add(h);
  103.             }
  104.  
  105.             init();
  106.  
  107.             ProblemExecution.start(threads, state);
  108.             System.out.println(new Date().getTime());
  109.         } catch (Exception ex) {
  110.             ex.printStackTrace();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment