Advertisement
Josif_tepe

Untitled

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