Advertisement
brsjak

Andrej Sol | OS - Volleyball Tournament | Septemvri 2019

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