Advertisement
brsjak

OS - Macau Card Tournament Problem

Jan 23rd, 2020
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.concurrent.Semaphore;
  3.  
  4.  
  5. public class MacauCardTournament {
  6.    
  7.     static Semaphore redEnter = new Semaphore(2);
  8.     static Semaphore greenEnter = new Semaphore(2);
  9.     static int players = 0;
  10.     static Semaphore lock = new Semaphore(1);
  11.     static Semaphore canPlay = new Semaphore(0);
  12.     static Semaphore nextRound = new Semaphore(0);
  13.     static Semaphore nextStart = new Semaphore(0);
  14.    
  15.     static class GreenPlayer extends Thread {
  16.        
  17.  
  18.         public void execute() throws InterruptedException {
  19.             System.out.println("Green player ready");
  20.             Thread.sleep(50);
  21.             greenEnter.acquire();
  22.             lock.acquire();
  23.             System.out.println("Green player here");
  24.             players++;
  25.             if(players==4) {
  26.                 canPlay.release(4);
  27.             }
  28.             lock.release();
  29.             canPlay.acquire();
  30.            
  31.             for(int num=1;num<=3;num++) {
  32.                 // TODO: the following code should be executed 3 times
  33.                 lock.acquire();
  34.                 if(players==4) {
  35.                     nextStart.release(4);
  36.                 }
  37.                 lock.release();
  38.                
  39.                 nextStart.acquire();
  40.                 System.out.println("Green Game "+ num +" started");
  41.                 Thread.sleep(200);
  42.                 System.out.println("Green player finished game "+ num);
  43.                
  44.                 lock.acquire();
  45.                 --players;
  46.                 if(players==0) {
  47.                     if(num==3) {
  48.                         // TODO: only one player calls the next line per match
  49.                         nextRound.drainPermits();
  50.                         //nextRound.acquire(nextRound.availablePermits());
  51.                         System.out.println("Match finished");
  52.                         greenEnter.release(2);
  53.                         redEnter.release(2);
  54.                        
  55.                     } else {
  56.                         //TODO: only one player calls the next line per game
  57.                         System.out.println("Game "+ num +" finished");
  58.                         players=4;
  59.                         nextRound.release(4);
  60.                     }
  61.                 }
  62.                 lock.release();
  63.                
  64.                 nextRound.acquire();
  65.             }
  66.            
  67.         }
  68.    
  69.        
  70.         public void run() {
  71.             try {
  72.                 execute();
  73.             }catch(Exception e) {
  74.                 e.printStackTrace();
  75.             }
  76.         }
  77.  
  78.     }
  79.  
  80.     static class RedPlayer extends Thread{
  81.  
  82.  
  83.  
  84.         public void execute() throws InterruptedException {
  85.             System.out.println("Red player ready");
  86.             Thread.sleep(50);
  87.             redEnter.acquire();
  88.             lock.acquire();
  89.             System.out.println("Red player here");
  90.             System.out.println("Players:" + players);
  91.             players++;
  92.             if(players==4) {
  93.                 canPlay.release(4);
  94.             }
  95.             lock.release();
  96.             canPlay.acquire();
  97.            
  98.             for(int num=1;num<=3;num++) {
  99.                 lock.acquire();
  100.                 if(players==4) {
  101.                     nextStart.release(4);
  102.                 }
  103.                 lock.release();
  104.                 // TODO: the following code should be executed 3 times
  105.                 System.out.println("Red Game "+ num +" started");
  106.                 Thread.sleep(200);
  107.                 System.out.println("Red player finished game "+ num);
  108.                
  109.                 lock.acquire();
  110.                 --players;
  111.                 if(players==0) {
  112.                     if(num==3) {
  113.                         // TODO: only one player calls the next line per match
  114.                         nextRound.drainPermits();
  115.                         //nextRound.acquire(nextRound.availablePermits());
  116.                         System.out.println("Match finished");
  117.                         redEnter.release(2);
  118.                         greenEnter.release(2);
  119.                        
  120.                     } else {
  121.                         //TODO: only one player calls the next line per game
  122.                         System.out.println("Game "+ num +" finished");
  123.                         players=4;
  124.                         nextRound.release(4);
  125.                     }
  126.                 }
  127.                 lock.release();
  128.                 nextRound.acquire();
  129.             }
  130.            
  131.            
  132.         }
  133.         public void run() {
  134.             try {
  135.                 execute();
  136.             }catch(Exception e) {
  137.                 e.printStackTrace();
  138.             }
  139.         }
  140.     }
  141.  
  142.  
  143.     public static void main(String[] args) throws InterruptedException {
  144.         HashSet<Thread> threads = new HashSet<Thread>();
  145.         for (int i = 0; i < 30; i++) {
  146.             RedPlayer red = new RedPlayer();
  147.             threads.add(red);
  148.             GreenPlayer green = new GreenPlayer();
  149.             threads.add(green);
  150.         }
  151.         //start 30 red and 30 green players in background
  152.         for(Thread t : threads) {
  153.             t.start();
  154.         }
  155.         // after all of them are started, wait each of them to finish for 1_000 ms
  156.         for(Thread t : threads) {
  157.             t.join(100);
  158.         }
  159.         //after the waiting for each of the players is done, check the one that are not finished and terminate them
  160.         for(Thread t : threads) {
  161.             if(t.isAlive()) {
  162.                 t.interrupt();
  163.                  System.err.println("Possible deadlock");
  164.             }
  165.         }
  166.        
  167.        
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement