Advertisement
NHristovski

Untitled

Mar 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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 = 1000;
  8.  
  9. int f1count;
  10. int f2count;
  11. int maxDifference = 0;
  12. Semaphore canExecuteF1;
  13. Semaphore f1done;
  14.  
  15. /**
  16. * Metod koj treba da gi inicijalizira vrednostite na semaforite i
  17. * ostanatite promenlivi za sinhronizacija.
  18. *
  19. */
  20. public void init(int count) {
  21. //da se implementira
  22. canExecuteF1 = new Semaphore(count);
  23. f1done = new Semaphore(0);
  24. }
  25.  
  26. class F1Thread extends Thread {
  27.  
  28. public void executeF1() throws InterruptedException {
  29. //da se implementira
  30. canExecuteF1.acquire();
  31. f1();
  32. f1done.release();
  33. }
  34.  
  35. @Override
  36. public void run() {
  37. try {
  38. executeF1();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44.  
  45. class F2Thread extends Thread {
  46.  
  47. public void executeF2() throws InterruptedException {
  48. //da se implementira
  49. f1done.acquire();
  50. f2();
  51. canExecuteF1.release();
  52. }
  53.  
  54. @Override
  55. public void run() {
  56. try {
  57. executeF2();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. public void f1() {
  65. System.out.println("f1()");
  66. f1count++;
  67. if (f1count - f2count > maxDifference) {
  68. maxDifference = f1count - f2count;
  69. }
  70. }
  71.  
  72. public void f2() {
  73. System.out.println("f2()");
  74. f2count++;
  75.  
  76. if (f1count - f2count > maxDifference) {
  77. maxDifference = f1count - f2count;
  78. }
  79. }
  80.  
  81. public static void main(String[] args) {
  82. try {
  83. Naizmenicno environment = new Naizmenicno();
  84. environment.start();
  85. } catch (Exception ex) {
  86. ex.printStackTrace();
  87. }
  88. }
  89.  
  90. public void start() throws Exception {
  91.  
  92. System.out.println("Vnesete za kolku poveke sakate da se izvrsi f1()");
  93. Scanner s = new Scanner(System.in);
  94. int n = s.nextInt();
  95. init(n);
  96.  
  97. HashSet<Thread> threads = new HashSet<Thread>();
  98. for (int i = 0; i < NUM_RUNS; i++) {
  99. F1Thread f1 = new F1Thread();
  100. F2Thread f2 = new F2Thread();
  101. threads.add(f1);
  102. threads.add(f2);
  103. }
  104.  
  105. for (Thread t : threads) {
  106. t.start();
  107. }
  108.  
  109. for (Thread t : threads) {
  110. t.join();
  111. }
  112. System.out.println("F1count: " + f1count);
  113. System.out.println("F2count: " + f2count);
  114. System.out.println("maxDifference: " + maxDifference);
  115. System.out.println("Status: " + (maxDifference <= n));
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement