Advertisement
Davor97

Lab2_Threads_z3

Sep 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3. import java.util.concurrent.Semaphore;
  4.  
  5. public class Naizmenicno {
  6.  
  7. public static int NUM_RUNS = 10000;
  8.  
  9. int f1count;
  10. int f2count;
  11. int maxDifference = 0;
  12.  
  13. /**
  14. * Metod koj treba da gi inicijalizira vrednostite na semaforite i
  15. * ostanatite promenlivi za sinhronizacija.
  16. *
  17. */
  18. Semaphore sem1;
  19. Semaphore sem2;
  20.  
  21.  
  22. public void init(int count) {
  23. maxDifference = count;
  24. f1count = 0;
  25. f2count = 0;
  26. //f1 ke se isprinta max count pati a posle ke mora f2 za da mu relesova pa da moze pak f1
  27. sem1 = new Semaphore(count);
  28. //so sekoe izvrsuvanje na f1, f2 dobiva release i dozvola za izvrsuvanje
  29. sem2 = new Semaphore(0);
  30. }
  31.  
  32. class F1Thread extends Thread {
  33.  
  34. public void executeF1() throws InterruptedException {
  35. sem1.acquire();
  36. //potreben e mutex bidejki f1 i f2 koristat spodeleni promenlivi od istata klasa.....
  37. synchronized (Naizmenicno.class) {
  38. f1();
  39. }
  40.  
  41. sem2.release();
  42.  
  43. }
  44.  
  45. @Override
  46. public void run() {
  47. try {
  48. executeF1();
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54.  
  55. class F2Thread extends Thread {
  56.  
  57. public void executeF2() throws InterruptedException {
  58. sem2.acquire();
  59. synchronized (Naizmenicno.class) {
  60. f2();
  61. }
  62.  
  63. sem1.release();
  64. }
  65.  
  66. @Override
  67. public void run() {
  68. try {
  69. executeF2();
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75.  
  76. public void f1() {
  77. System.out.println("f1()");
  78. f1count++;
  79. if (f1count - f2count > maxDifference) {
  80. maxDifference = f1count - f2count;
  81. }
  82. }
  83.  
  84. public void f2() {
  85. System.out.println("f2()");
  86. f2count++;
  87.  
  88. if (f1count - f2count > maxDifference) {
  89. maxDifference = f1count - f2count;
  90. }
  91. }
  92.  
  93. public static void main(String[] args) {
  94. try {
  95. Naizmenicno environment = new Naizmenicno();
  96. environment.start();
  97.  
  98.  
  99. } catch (Exception ex) {
  100. ex.printStackTrace();
  101. }
  102. }
  103.  
  104. public void start() throws Exception {
  105.  
  106. System.out.println("Vnesete za kolku poveke sakate da se izvrsi f1()");
  107. Scanner s = new Scanner(System.in);
  108. int n = s.nextInt();
  109. init(n);
  110.  
  111. HashSet<Thread> threads = new HashSet<Thread>();
  112. for (int i = 0; i < NUM_RUNS; i++) {
  113. F1Thread f1 = new F1Thread();
  114. F2Thread f2 = new F2Thread();
  115. threads.add(f1);
  116. threads.add(f2);
  117. }
  118.  
  119. for (Thread t : threads) {
  120. t.start();
  121. }
  122.  
  123. for (Thread t : threads) {
  124. t.join();
  125. }
  126. System.out.println("F1count: " + f1count);
  127. System.out.println("F2count: " + f2count);
  128. System.out.println("maxDifference: " + maxDifference);
  129. System.out.println("Status: " + (maxDifference <= n));
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement