Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. package lab;
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8.  
  9. import java.util.concurrent.Semaphore;
  10. /**
  11. *
  12. * @author ZTI
  13. */
  14. /********************************
  15. * Zadanie polega na zsynchronizowaniu wątków P1, P2, P3 oraz P4
  16. * tak aby suma zmiennych A, B i C wyświetlana w wątku P4 osiągnęła
  17. * określoną wartość (same zmienne mogą mieć wartości dowolne, ale sumujące się do zadanej).
  18. * UWAGA: Wykonanie kodu klasy nie powinno się kończyć blokadą tzn.
  19. * wszystkie wątki powinny zakończyć swoje działanie,
  20. * nawet jeśli watek P4 zakończy się wcześniej.
  21. * Powyższe zachowanie należy zapewnić używając jak najmniejszej liczby semaforów.
  22. * Zestawy zadań (zawierają dwie wartości sumy):^
  23. 1) 16, 60
  24. 2) 21, 56
  25. 3) 22, 46
  26. 4) 26, 40
  27. 5) 30, 36
  28. 6) 36, 30
  29. 7) 40, 22
  30. 8) 46, 26
  31. 9) 56, 16
  32. 10) 60, 21
  33. 11) 16, 46
  34. 12) 21, 40
  35. 13) 22, 36
  36. 14) 26, 30
  37. 15) 30, 60
  38. 16) 36, 56
  39. 17) 40, 16
  40. 18) 46, 21
  41. 19) 56, 22
  42. 20) 60, 26
  43. **********************************/
  44. public class Zad_EP {
  45.  
  46. private static int A = 0;
  47. private static int B = 0;
  48. private static int C = 3;
  49.  
  50. private static final Semaphore P1 = new Semaphore(1, true);
  51. private static final Semaphore P2 = new Semaphore(0, true);
  52. private static final Semaphore P3 = new Semaphore(0, true);
  53. private static final Semaphore P4 = new Semaphore(0, true);
  54.  
  55. public static void main(String[] args) {
  56. new P1().start();
  57. new P2().start();
  58. new P3().start();
  59. new P4().start();
  60. }
  61.  
  62. private static final class P1 extends Thread {
  63.  
  64. @Override
  65. public void run() {
  66. try {
  67. P1.acquire();
  68. A = 10;
  69. P3.release();
  70. P1.acquire();
  71. P2.release();
  72. B = B + 5;
  73. C = C + A;
  74.  
  75.  
  76. Thread.sleep(0);
  77. System.out.println("Thread P1 is done...");
  78.  
  79. } catch (InterruptedException ex) {
  80. System.out.println("Ooops...");
  81. Thread.currentThread().interrupt();
  82. throw new RuntimeException(ex);
  83. }
  84. }
  85. }
  86.  
  87. private static final class P2 extends Thread {
  88.  
  89. @Override
  90. public void run() {
  91. try {
  92. P2.acquire();
  93. B = B + C;
  94. P4.release();
  95. P2.acquire();
  96. P3.release();
  97. A = A + B;
  98. Thread.sleep(0);
  99.  
  100. System.out.println("Thread P2 is done...");
  101.  
  102. } catch (InterruptedException ex) {
  103. System.out.println("Ooops...");
  104. Thread.currentThread().interrupt();
  105. throw new RuntimeException(ex);
  106. }
  107. }
  108. }
  109.  
  110. private static final class P3 extends Thread {
  111.  
  112. @Override
  113. public void run() {
  114. try {
  115. P3.acquire();
  116. C = B + 10;
  117.  
  118. A = 2 * A;
  119. P2.release();
  120. P3.acquire();
  121. B = B + A;
  122.  
  123.  
  124.  
  125.  
  126. Thread.sleep(0);
  127. System.out.println("Thread P3 is done...");
  128.  
  129. } catch (InterruptedException ex) {
  130. System.out.println("Ooops...");
  131. Thread.currentThread().interrupt();
  132. throw new RuntimeException(ex);
  133. }
  134. }
  135. }
  136.  
  137. private static final class P4 extends Thread {
  138.  
  139. @Override
  140. public void run() {
  141. try {
  142. P4.acquire();
  143. System.out.println("Sum result: " + A + " + " + B + " + " + C + " = " + (A + B + C));
  144. P1.release();
  145. Thread.sleep(0);
  146. System.out.println("Thread P4 is done...");
  147.  
  148. } catch (InterruptedException ex) {
  149. System.out.println("Ooops...");
  150. Thread.currentThread().interrupt();
  151. throw new RuntimeException(ex);
  152. }
  153. }
  154. }
  155.  
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement