prometheus800

ОС Лаб 3: "Basketball Tournament"

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