Martina312

ОС - Tennis Tournament

Apr 23rd, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package ispiti.tennisTournament;
  2.  
  3. import java.util.HashSet;
  4. import java.util.concurrent.Semaphore;
  5.  
  6. public class TennisTournament {
  7.  
  8.     static Semaphore redPlayers;
  9.     static Semaphore greenPlayers;
  10.     static int numPlayers;
  11.     static Semaphore protect;
  12.     static Semaphore canPlay;
  13.     static Semaphore finishedPlaying;
  14.  
  15.     public static void init(){
  16.         redPlayers = new Semaphore(2);
  17.         greenPlayers = new Semaphore(2);
  18.         numPlayers = 0;
  19.         protect = new Semaphore(1);
  20.         canPlay = new Semaphore(0);
  21.         finishedPlaying = new Semaphore(0);
  22.  
  23.     }
  24.  
  25.     public static class GreenPlayer extends Thread {
  26.  
  27.  
  28.         public void execute() throws InterruptedException {
  29.            // System.out.println("Green player ready");
  30.  
  31.             greenPlayers.acquire();
  32.             System.out.println("Green player enters field");
  33.             protect.acquire();
  34.             numPlayers++;
  35.             boolean controller = false;
  36.             if(numPlayers == 4){
  37.                 controller = true;
  38.                 canPlay.release(4);
  39.             }
  40.             protect.release();
  41.  
  42.             canPlay.acquire();
  43.             System.out.println("Match started");
  44.             Thread.sleep(200);
  45.             System.out.println("Green player finished playing");
  46.             finishedPlaying.release();
  47.             if(controller) {
  48.                 finishedPlaying.acquire(4);
  49.                 System.out.println("Match finished");
  50.                 numPlayers = 0;
  51.                 greenPlayers.release(2);
  52.                 redPlayers.release(2);
  53.             }
  54.         }
  55.  
  56.         @Override
  57.         public void run() {
  58.             try {
  59.                 execute();
  60.             } catch (InterruptedException e) {
  61.                 e.printStackTrace();
  62.             }
  63.         }
  64.     }
  65.  
  66.     public static class RedPlayer extends Thread {
  67.  
  68.         @Override
  69.         public void run() {
  70.             try {
  71.                 execute();
  72.             } catch (InterruptedException e) {
  73.                 e.printStackTrace();
  74.             }
  75.         }
  76.  
  77.         public void execute() throws InterruptedException {
  78.          //   System.out.println("Red player ready");
  79.  
  80.             redPlayers.acquire();
  81.             System.out.println("Red player enters field");
  82.             protect.acquire();
  83.             numPlayers++;
  84.             boolean controller = false;
  85.             if(numPlayers == 4){
  86.                 controller = true;
  87.                 canPlay.release(4);
  88.             }
  89.             protect.release();
  90.  
  91.             canPlay.acquire();
  92.             System.out.println("Match started");
  93.             Thread.sleep(200);
  94.             System.out.println("Red player finished playing");
  95.             finishedPlaying.release();
  96.  
  97.             if(controller) {
  98.                 finishedPlaying.acquire(4);
  99.                 System.out.println("Match finished");
  100.                 numPlayers = 0;
  101.                 greenPlayers.release(2);
  102.                 redPlayers.release(2);
  103.             }
  104.         }
  105.  
  106.     }
  107.  
  108.  
  109.     public static void main(String[] args) throws InterruptedException {
  110.         HashSet<Thread> threads = new HashSet<Thread>();
  111.         for (int i = 0; i < 30; i++) {
  112.             RedPlayer red = new RedPlayer();
  113.             threads.add(red);
  114.             GreenPlayer green = new GreenPlayer();
  115.             threads.add(green);
  116.         }
  117.         init();
  118.         // start 30 red and 30 green players in background
  119.         for(Thread t:threads){
  120.             t.start();
  121.         }
  122.         // after all of them are started, wait each of them to finish for 1_000 ms
  123.         for(Thread t:threads){
  124.             t.join();
  125.         }
  126.         // after the waiting for each of the players is done, check the one that are not finished and terminate them
  127.         System.err.println("Uspeshna sinhronizacija");
  128.     }
  129.  
  130. }
Add Comment
Please, Sign In to add comment