Advertisement
Guest User

Untitled

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