Advertisement
lameski

Untitled

Mar 30th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 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.Semaphore;
  9. import java.util.concurrent.locks.Lock;
  10. import java.util.concurrent.locks.ReentrantLock;
  11.  
  12. public class OxalicAcid {
  13. static Semaphore semC, semH, semO;
  14. static Semaphore semHereC, semHereH, semHereO;
  15. static Semaphore semReady, semDone;
  16. static Semaphore semNext;
  17. static int c,h,o;
  18.  
  19. static Lock lock;
  20.  
  21. public static void init() {
  22. semC = new Semaphore(2);
  23. semH = new Semaphore(2);
  24. semO = new Semaphore(4);
  25. semHereO = new Semaphore(0);
  26. semHereH = new Semaphore(0);
  27. semHereC = new Semaphore(0);
  28. semReady = new Semaphore(7);
  29. semDone = new Semaphore(0);
  30. semNext = new Semaphore(0);
  31. c = h = o = 0;
  32.  
  33. lock = new ReentrantLock();
  34.  
  35. }
  36.  
  37. public static class Carbon extends TemplateThread {
  38.  
  39. public Carbon(int numRuns) {
  40. super(numRuns);
  41. }
  42.  
  43. @Override
  44. public void execute() throws InterruptedException {
  45. semC.acquire();
  46. lock.lock();
  47. c++;
  48. if(c==2)
  49. {
  50. c=0;
  51. lock.unlock();
  52. semHereC.acquire();
  53. semHereH.acquire(2);
  54. semHereO.acquire(4);
  55. semReady.release(7);
  56. state.bond();
  57. semDone.acquire(7);
  58. semNext.release(7);
  59. state.validate();
  60.  
  61. }
  62. else
  63. {
  64. lock.unlock();
  65. semHereC.release();
  66. semReady.acquire();
  67. state.bond();
  68. semDone.release();
  69. semNext.acquire();
  70.  
  71. }
  72. semC.release();
  73.  
  74. }
  75.  
  76. }
  77.  
  78. public static class Hydrogen extends TemplateThread {
  79.  
  80. public Hydrogen(int numRuns) {
  81. super(numRuns);
  82. }
  83.  
  84. @Override
  85. public void execute() throws InterruptedException {
  86. semH.acquire();
  87. semHereH.release();
  88. semReady.acquire();
  89. state.bond();
  90. semDone.release();
  91. semNext.acquire();
  92. semH.release();
  93.  
  94. }
  95.  
  96. }
  97.  
  98. public static class Oxygen extends TemplateThread {
  99.  
  100. public Oxygen(int numRuns) {
  101. super(numRuns);
  102. }
  103.  
  104. @Override
  105. public void execute() throws InterruptedException {
  106. semO.acquire();
  107. semHereO.release();
  108. semReady.acquire();
  109. state.bond();
  110. semDone.release();
  111. semNext.acquire();
  112. semO.release();
  113.  
  114. }
  115.  
  116. }
  117.  
  118. static OxalicAcidState state = new OxalicAcidState();
  119.  
  120. public static void main(String[] args) {
  121. for (int i = 0; i < 10; i++) {
  122. run();
  123. }
  124. }
  125.  
  126. public static void run() {
  127. try {
  128. int numRuns = 1;
  129. int numScenarios = 300;
  130.  
  131. HashSet<Thread> threads = new HashSet<Thread>();
  132.  
  133. for (int i = 0; i < numScenarios; i++) {
  134. Oxygen o = new Oxygen(numRuns);
  135.  
  136. threads.add(o);
  137. if (i % 2 == 0) {
  138. Hydrogen h = new Hydrogen(numRuns);
  139. Carbon c = new Carbon(numRuns);
  140. threads.add(c);
  141. threads.add(h);
  142. }
  143. }
  144.  
  145. init();
  146.  
  147. ProblemExecution.start(threads, state);
  148. System.out.println(new Date().getTime());
  149. } catch (Exception ex) {
  150. ex.printStackTrace();
  151. }
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement