Advertisement
Guest User

Untitled

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