Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 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. P3.release();
  94. B = B + C;
  95.  
  96. A = A + B;
  97. Thread.sleep(0);
  98.  
  99. System.out.println("Thread P2 is done...");
  100.  
  101. } catch (InterruptedException ex) {
  102. System.out.println("Ooops...");
  103. Thread.currentThread().interrupt();
  104. throw new RuntimeException(ex);
  105. }
  106. }
  107. }
  108.  
  109. private static final class P3 extends Thread {
  110.  
  111. @Override
  112. public void run() {
  113. try {
  114. P3.acquire();
  115. C = B + 10;
  116.  
  117. A = 2 * A;
  118. P4.release();
  119. P3.acquire();
  120. B = B + A;
  121.  
  122.  
  123.  
  124.  
  125. Thread.sleep(0);
  126. System.out.println("Thread P3 is done...");
  127.  
  128. } catch (InterruptedException ex) {
  129. System.out.println("Ooops...");
  130. Thread.currentThread().interrupt();
  131. throw new RuntimeException(ex);
  132. }
  133. }
  134. }
  135.  
  136. private static final class P4 extends Thread {
  137.  
  138. @Override
  139. public void run() {
  140. try {
  141. P4.acquire();
  142. System.out.println("Sum result: " + A + " + " + B + " + " + C + " = " + (A + B + C));
  143. P1.release();
  144. Thread.sleep(0);
  145. System.out.println("Thread P4 is done...");
  146.  
  147. } catch (InterruptedException ex) {
  148. System.out.println("Ooops...");
  149. Thread.currentThread().interrupt();
  150. throw new RuntimeException(ex);
  151. }
  152. }
  153. }
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement