Advertisement
lameski

Untitled

Mar 30th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. package mk.ukim.finki.os.synchronization.exam14.june;
  2.  
  3. import mk.ukim.finki.os.synchronization.ProblemExecution;
  4. import mk.ukim.finki.os.synchronization.TemplateThread;
  5.  
  6. import java.util.Date;
  7. import java.util.HashSet;
  8. import java.util.concurrent.RejectedExecutionException;
  9. import java.util.concurrent.Semaphore;
  10. import java.util.concurrent.locks.Lock;
  11. import java.util.concurrent.locks.ReentrantLock;
  12.  
  13. public class Euro2016 {
  14. static Semaphore semA, semB;
  15. static Semaphore semDojdA, semDojdB;
  16. static Semaphore semBoard;
  17. static Semaphore semReady;
  18. static Semaphore semDone;
  19. static Semaphore semNext;
  20. static int a, b;
  21. static Lock lock = new ReentrantLock();
  22. public static void init() {
  23. semA = new Semaphore(4);
  24. semB = new Semaphore(4);
  25. semDojdA = new Semaphore(0);
  26. semDojdB = new Semaphore(0);
  27. semBoard = new Semaphore(0);
  28. semReady = new Semaphore(4);
  29. semDone = new Semaphore(0);
  30. semNext = new Semaphore(0);
  31. a = b = 0;
  32. }
  33.  
  34. static class FanA extends TemplateThread {
  35.  
  36. public FanA(int numRuns) {
  37. super(numRuns);
  38. }
  39.  
  40. public void execute() throws InterruptedException {
  41. while(true){
  42. lock.lock();
  43. boolean mestoZaB = ( b==3 || (a==2 && b==1));
  44. if(!mestoZaB)
  45. {
  46. //semA.acquire();
  47. a++;
  48. state.board();
  49. if(a+b==4)
  50. {
  51. state.departure();
  52. a=b=0;
  53. }
  54. break;
  55.  
  56. }
  57. lock.unlock();
  58. }
  59. lock.unlock();
  60.  
  61. }
  62. }
  63.  
  64. static class FanB extends TemplateThread {
  65.  
  66. public FanB(int numRuns) {
  67. super(numRuns);
  68. }
  69.  
  70. public void execute() throws InterruptedException {
  71. while(true){
  72. lock.lock();
  73. boolean mestoZaA = ( a==3 || (a==1 && b==2));
  74. if(!mestoZaA)
  75. {
  76. //semB.acquire();
  77. b++;
  78. state.board();
  79. if(a+b==4)
  80. {
  81. state.departure();
  82. a=b=0;
  83. }
  84. break;
  85.  
  86. }
  87. lock.unlock();
  88. }
  89. lock.unlock();
  90.  
  91. }
  92.  
  93.  
  94. }
  95.  
  96. static Euro2016State state = new Euro2016State();
  97.  
  98. public static void main(String[] args) {
  99. for (int i = 0; i < 15; i++)
  100. run();
  101. }
  102.  
  103. public static void run() {
  104. try {
  105. int numRuns = 1;
  106. int numIterations = 120;
  107.  
  108. HashSet<Thread> threads = new HashSet<Thread>();
  109.  
  110. for (int i = 0; i < numIterations; i++) {
  111. FanA h = new FanA(numRuns);
  112. FanB s = new FanB(numRuns);
  113. threads.add(h);
  114. threads.add(s);
  115. }
  116.  
  117. init();
  118.  
  119. ProblemExecution.start(threads, state);
  120. System.out.println(new Date().getTime());
  121. } catch (Exception ex) {
  122. ex.printStackTrace();
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement