Advertisement
brsjak

OS Synchronization - Volleyball Tournament | September 2019

Jan 18th, 2020
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.concurrent.Semaphore;
  3.  
  4. public class VolleyballTournament {
  5.    
  6.    
  7.    
  8.    
  9.     public static class Player extends Thread{
  10.         int tdx;
  11.        
  12.         static Semaphore sala = new Semaphore(12);
  13.         static Semaphore soblekuvalna = new Semaphore(4);
  14.         static int count = 0;
  15.         static int gCount = 0;
  16.         static Semaphore cLock = new Semaphore(1);
  17.         static Semaphore canPlay = new Semaphore(0);
  18.         static Semaphore eHall = new Semaphore(0);
  19.         static boolean flag=false;
  20.        
  21.        
  22.         public Player(int tdx) {
  23.             this.tdx=tdx;
  24.         }
  25.        
  26.         public void execute() throws InterruptedException {
  27.             // at most 12 players should print this in parallel
  28.             sala.acquire();
  29.             System.out.println("Player inside.");
  30.             // at most 4 players may enter in the dressing room in parallel
  31.             soblekuvalna.acquire();
  32.             System.out.println("In dressing room.");
  33.            
  34.             cLock.acquire();
  35.             count++;
  36.             gCount++;
  37.             if(count==4) {
  38.                 count=0;
  39.                 eHall.release(4);
  40.                 flag = true;
  41.             }
  42.            
  43.             Thread.sleep(10);// this represent the dressing time
  44.             if(gCount==12) {
  45.                 canPlay.release(12);
  46.             }
  47.             if(flag) {
  48.                 soblekuvalna.release(4);
  49.                 flag=false;
  50.             }
  51.             cLock.release();
  52.             eHall.acquire();   
  53.             //System.out.println("Player entered the hall");
  54.             canPlay.acquire();
  55.             // after all players are ready, they should start with the game together
  56.             System.out.println("Game started.");
  57.             Thread.sleep(100);// this represent the game duration
  58.             cLock.acquire();
  59.             --gCount;
  60.             if(gCount==0) {
  61.                 System.out.println("Game finished.");
  62.                 sala.release(12);
  63.                 eHall.drainPermits();
  64.                 canPlay.drainPermits();
  65.                 count=0;
  66.             } else {
  67.                 System.out.println("Player done.");
  68.             }
  69.             cLock.release();
  70.            
  71.            
  72.            
  73.             // only one player should print the next line, representing that the game has finished
  74.            
  75.         }
  76.        
  77.         public void run() {
  78.             try {
  79.                 execute();
  80.                
  81.             }catch(InterruptedException e) {
  82.                 e.printStackTrace();
  83.             }
  84.         }
  85.     }
  86.    
  87.  
  88.  
  89.     public static void main(String[] args) throws InterruptedException {
  90.         HashSet<Player> threads = new HashSet<>();
  91.         for (int i = 0; i < 60; i++) {
  92.             Player p = new Player(i);
  93.             threads.add(p);
  94.         }
  95.         // run all threads in background
  96.        
  97.         for(Player t : threads) {
  98.             t.start();
  99.         }
  100.         // after all of them are started, wait each of them to finish for maximum 2_000 ms
  101.         for(Player t : threads) {
  102.             t.join(2000);
  103.         }
  104.         // for each thread, terminate it if it is not finished
  105.         for(Player t : threads) {
  106.             if(t.isAlive()) {
  107.                 t.interrupt();
  108.                 System.out.println("Possible deadlock!");
  109.             }
  110.         }
  111.         System.out.println("Tournament finished.");
  112.  
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement