Advertisement
Guest User

votra treta

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