Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.concurrent.Semaphore;
- public class MacauCardTournament {
- static Semaphore redEnter = new Semaphore(2);
- static Semaphore greenEnter = new Semaphore(2);
- static int players = 0;
- static Semaphore lock = new Semaphore(1);
- static Semaphore canPlay = new Semaphore(0);
- static Semaphore nextRound = new Semaphore(0);
- static Semaphore nextStart = new Semaphore(0);
- static class GreenPlayer extends Thread {
- public void execute() throws InterruptedException {
- System.out.println("Green player ready");
- Thread.sleep(50);
- greenEnter.acquire();
- lock.acquire();
- System.out.println("Green player here");
- players++;
- if(players==4) {
- canPlay.release(4);
- }
- lock.release();
- canPlay.acquire();
- for(int num=1;num<=3;num++) {
- // TODO: the following code should be executed 3 times
- lock.acquire();
- if(players==4) {
- nextStart.release(4);
- }
- lock.release();
- nextStart.acquire();
- System.out.println("Green Game "+ num +" started");
- Thread.sleep(200);
- System.out.println("Green player finished game "+ num);
- lock.acquire();
- --players;
- if(players==0) {
- if(num==3) {
- // TODO: only one player calls the next line per match
- nextRound.drainPermits();
- //nextRound.acquire(nextRound.availablePermits());
- System.out.println("Match finished");
- greenEnter.release(2);
- redEnter.release(2);
- } else {
- //TODO: only one player calls the next line per game
- System.out.println("Game "+ num +" finished");
- players=4;
- nextRound.release(4);
- }
- }
- lock.release();
- nextRound.acquire();
- }
- }
- public void run() {
- try {
- execute();
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- static class RedPlayer extends Thread{
- public void execute() throws InterruptedException {
- System.out.println("Red player ready");
- Thread.sleep(50);
- redEnter.acquire();
- lock.acquire();
- System.out.println("Red player here");
- System.out.println("Players:" + players);
- players++;
- if(players==4) {
- canPlay.release(4);
- }
- lock.release();
- canPlay.acquire();
- for(int num=1;num<=3;num++) {
- lock.acquire();
- if(players==4) {
- nextStart.release(4);
- }
- lock.release();
- // TODO: the following code should be executed 3 times
- System.out.println("Red Game "+ num +" started");
- Thread.sleep(200);
- System.out.println("Red player finished game "+ num);
- lock.acquire();
- --players;
- if(players==0) {
- if(num==3) {
- // TODO: only one player calls the next line per match
- nextRound.drainPermits();
- //nextRound.acquire(nextRound.availablePermits());
- System.out.println("Match finished");
- redEnter.release(2);
- greenEnter.release(2);
- } else {
- //TODO: only one player calls the next line per game
- System.out.println("Game "+ num +" finished");
- players=4;
- nextRound.release(4);
- }
- }
- lock.release();
- nextRound.acquire();
- }
- }
- public void run() {
- try {
- execute();
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- HashSet<Thread> threads = new HashSet<Thread>();
- for (int i = 0; i < 30; i++) {
- RedPlayer red = new RedPlayer();
- threads.add(red);
- GreenPlayer green = new GreenPlayer();
- threads.add(green);
- }
- //start 30 red and 30 green players in background
- for(Thread t : threads) {
- t.start();
- }
- // after all of them are started, wait each of them to finish for 1_000 ms
- for(Thread t : threads) {
- t.join(100);
- }
- //after the waiting for each of the players is done, check the one that are not finished and terminate them
- for(Thread t : threads) {
- if(t.isAlive()) {
- t.interrupt();
- System.err.println("Possible deadlock");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement