Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. package mk.ukim.finki.os.synchronization.exam16.s2;
  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.  
  10. public class OxalicAcid {
  11.  
  12.  
  13.  
  14. //#########
  15. //Mart Cube
  16. //#########
  17.  
  18. public static void init() {
  19. }
  20.  
  21. static int C = 0;
  22. static int H = 0;
  23. static int O = 0;
  24. static Semaphore carbon = new Semaphore(2);
  25. static Semaphore hydrogen = new Semaphore(2);
  26. static Semaphore oxygen = new Semaphore(4);
  27.  
  28. static Semaphore start_bond = new Semaphore(0);
  29.  
  30.  
  31. public static class Carbon extends TemplateThread {
  32. public Carbon(int numRuns) { super(numRuns); }
  33. @Override
  34. public void execute() throws InterruptedException {
  35.  
  36.  
  37. carbon.acquire();
  38. synchronized (carbon) {
  39. C++;
  40. if( C==2 && H==2 && O==4){ // posledniot atom sto vleguva od C H O toj ke gi pusti site ( togas ke bide se spremno za bond )
  41. start_bond.release(8); // vkupno C H i O se 8 atomi
  42. }
  43. }
  44.  
  45. start_bond.acquire(); // site cekaat tuka
  46. state.bond(); // zaedno site go izvrsuvaat koga ke mu bide dozvoleno
  47.  
  48. synchronized (carbon) {
  49. C--;
  50. if( C==0 && H==0 && O==0){
  51. state.validate(); // posledniot atom pravi validacija
  52.  
  53. // reset
  54. carbon.release(2);
  55. hydrogen.release(2);
  56. oxygen.release(4);
  57. }
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
  64. }
  65.  
  66. public static class Hydrogen extends TemplateThread {
  67. public Hydrogen(int numRuns) {super(numRuns); }
  68. @Override
  69. public void execute() throws InterruptedException {
  70.  
  71. hydrogen.acquire();
  72. synchronized (hydrogen) {
  73. H++;
  74. if( C==2 && H==2 && O==4){
  75. start_bond.release(8);
  76. }
  77. }
  78.  
  79. start_bond.acquire();
  80. state.bond();
  81.  
  82. synchronized (carbon) {
  83. H--;
  84. if( C==0 && H==0 && O==0){
  85. state.validate(); // the last atom is running this
  86.  
  87. // reset for new atom
  88. carbon.release(2);
  89. hydrogen.release(2);
  90. oxygen.release(4);
  91. }
  92. }
  93.  
  94.  
  95. }
  96.  
  97. }
  98.  
  99. public static class Oxygen extends TemplateThread {
  100. public Oxygen(int numRuns) {super(numRuns);}
  101. @Override
  102. public void execute() throws InterruptedException {
  103.  
  104.  
  105. oxygen.acquire();
  106. synchronized (carbon) {
  107. O++;
  108. if( C==2 && H==2 && O==4){
  109. start_bond.release(8);
  110. }
  111. }
  112.  
  113. start_bond.acquire();
  114. state.bond();
  115.  
  116. synchronized (carbon) {
  117. O--;
  118. if( C==0 && H==0 && O==0){
  119. state.validate(); // the last atom is running this
  120.  
  121. // reset for new atom
  122. carbon.release(2);
  123. hydrogen.release(2);
  124. oxygen.release(4);
  125. }
  126. }
  127.  
  128.  
  129.  
  130. }
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. static OxalicAcidState state = new OxalicAcidState();
  149. public static void main(String[] args) {
  150. for (int i = 0; i < 10; i++) {
  151. run();
  152. }
  153. }
  154. public static void run() {
  155. try {
  156. int numRuns = 1;
  157. int numScenarios = 300;
  158.  
  159. HashSet<Thread> threads = new HashSet<Thread>();
  160.  
  161. for (int i = 0; i < numScenarios; i++) {
  162. Oxygen o = new Oxygen(numRuns);
  163.  
  164. threads.add(o);
  165. if (i % 2 == 0) {
  166. Hydrogen h = new Hydrogen(numRuns);
  167. Carbon c = new Carbon(numRuns);
  168. threads.add(c);
  169. threads.add(h);
  170. }
  171. }
  172.  
  173. init();
  174.  
  175. ProblemExecution.start(threads, state);
  176. System.out.println(new Date().getTime());
  177. } catch (Exception ex) {
  178. ex.printStackTrace();
  179. }
  180. }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement