Advertisement
Guest User

Untitled

a guest
May 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. package semaphores;
  2.  
  3. import java.util.concurrent.Semaphore;
  4.  
  5. public final class ABCwithSem {
  6.  
  7. private static final int COUNT = 30;
  8. private static int COUNTER = 0;
  9. private static final int STEP = 10;
  10. private static final Semaphore a = new Semaphore(0 ,true);
  11. private static final Semaphore b = new Semaphore(0, true);
  12. private static final Semaphore c = new Semaphore(0, true);
  13. private static final Semaphore d = new Semaphore(0, true);
  14. private static final Semaphore e = new Semaphore(0, true);
  15. private static final Semaphore f = new Semaphore(1, true);
  16.  
  17.  
  18. public static void main(String[] args) {
  19. new A().start();
  20. new B().start();
  21. new C().start();
  22. new D().start();
  23. new E().start();
  24. new F().start();
  25. }
  26.  
  27. private static final class A extends Thread {
  28.  
  29. @Override
  30. public void run() {
  31. try {
  32. for (int i = 0; i < COUNT; i++) {
  33.  
  34. a.acquire();
  35. myPrint("A ");
  36. f.release();
  37. }
  38. } catch (InterruptedException ex) {
  39. }
  40. }
  41. }
  42.  
  43. private static final class B extends Thread {
  44.  
  45. @Override
  46. public void run() {
  47. try {
  48. for (int i = 0; i < COUNT*2; i++) {
  49. b.acquire();
  50. myPrint("B ");
  51. f.release();
  52. }
  53. } catch (InterruptedException ex) {
  54. }
  55. }
  56. }
  57.  
  58. private static final class C extends Thread {
  59.  
  60. @Override
  61. public void run() {
  62. try {
  63. for (int i = 0; i < COUNT; i++) {
  64.  
  65. c.acquire();
  66. myPrint("C ");
  67.  
  68. f.release();
  69. }
  70. } catch (InterruptedException ex) {
  71. }
  72. }
  73. }
  74.  
  75.  
  76.  
  77. private static final class D extends Thread {
  78.  
  79. @Override
  80. public void run() {
  81. try {
  82. for (int i = 0; i < COUNT; i++) {
  83.  
  84. d.acquire();
  85. myPrint("D ");
  86.  
  87. f.release();
  88. }
  89. } catch (InterruptedException ex) {
  90. }
  91. }
  92. }
  93.  
  94.  
  95. private static final class E extends Thread {
  96.  
  97. @Override
  98. public void run() {
  99. try {
  100. for (int i = 0; i < COUNT; i++) {
  101.  
  102. d.acquire();
  103. myPrint("E ");
  104.  
  105. f.release();
  106. }
  107. } catch (InterruptedException ex) {
  108. }
  109. }
  110. }
  111.  
  112.  
  113. private static final class F extends Thread {
  114.  
  115. @Override
  116. public void run() {
  117. try {
  118. for (int i = 0; i < COUNT; i++) {
  119.  
  120. f.acquire();
  121. d.release();
  122. f.acquire();
  123. c.release();
  124. f.acquire();
  125. b.release(2);
  126. f.acquire(2);
  127. a.release();
  128. f.acquire();
  129. c.release();
  130. f.acquire();
  131. e.release();
  132. f.acquire();
  133. a.release();
  134. f.acquire();
  135. b.release();
  136. f.acquire();
  137. e.release();
  138.  
  139.  
  140. }
  141. } catch (InterruptedException ex) {
  142. }
  143. }
  144. }
  145.  
  146.  
  147.  
  148. private static synchronized void myPrint(String s) {
  149. COUNTER++;
  150. System.out.print(s);
  151. if (COUNTER == STEP) {
  152. COUNTER = 0;
  153. System.out.println();
  154. }
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement