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 TennisTournament {
- static int red = 0;
- static int green = 0;
- static Semaphore lock = new Semaphore(1);
- static Semaphore redEnter = new Semaphore(2);
- static Semaphore greenEnter = new Semaphore(2);
- static Semaphore canPlay = new Semaphore(0);
- static boolean flag=false;
- public static class GreenPlayer extends Thread{
- public void execute() throws InterruptedException {
- System.out.println("Green player ready");
- greenEnter.acquire();
- lock.acquire();
- green++;
- if(red+green==4) {
- if(canPlay.availablePermits()==0) {
- canPlay.release(4);
- }
- }
- lock.release();
- System.out.println("Green player enters field");
- canPlay.acquire();
- System.out.println("Match started");
- Thread.sleep(200);
- lock.acquire();
- --green;
- if(green == 0 && red == 0 ) {
- System.out.println("Match finished");
- if(redEnter.availablePermits()==0 && greenEnter.availablePermits()==0) {
- redEnter.release(2);
- greenEnter.release(2);
- }
- } else {
- System.out.println("Green player finished playing");
- }
- lock.release();
- // TODO: only one player calls the next line per match
- }
- public void run() {
- try {
- execute();
- }catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static class RedPlayer extends Thread{
- public void execute() throws InterruptedException {
- System.out.println("Red player ready");
- redEnter.acquire();
- lock.acquire();
- red++;
- if(red+green==4) {
- if(canPlay.availablePermits()==0) {
- canPlay.release(4);
- }
- }
- lock.release();
- System.out.println("Red player enters field");
- canPlay.acquire();
- System.out.println("Match started");
- Thread.sleep(200);
- lock.acquire();
- --red;
- if(green == 0 && red == 0 ) {
- System.out.println("Match finished");
- if(redEnter.availablePermits()==0 && greenEnter.availablePermits()==0) {
- redEnter.release(2);
- greenEnter.release(2);
- }
- }else {
- System.out.println("Red player finished playing");
- }
- lock.release();
- }
- public void run() {
- try {
- execute();
- }catch(InterruptedException 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(1000);
- }
- for(Thread t : threads) {
- if(t.isAlive()) {
- t.interrupt();
- System.err.println("Possible deadlock");
- }
- }
- // after the waiting for each of the players is done, check the one that are not finished and terminate them
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement