JohnnSmith

AluminiumHydroxide.java

Mar 31st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package mk.ukim.finki.os.synchronization.exam14.march.aloh3;
  2.  
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.concurrent.Semaphore;
  6.  
  7. import mk.ukim.finki.os.synchronization.ProblemExecution;
  8. import mk.ukim.finki.os.synchronization.TemplateThread;
  9.  
  10. public class AluminiumHydroxide {
  11.  
  12.     static Semaphore o;
  13.     static Semaphore h;
  14.     static Semaphore al;
  15.  
  16.     static Semaphore ohHere;
  17.     static Semaphore oHere;
  18.  
  19.     static Semaphore ohReady;
  20.     static Semaphore ready;
  21.     static Semaphore done;
  22.     static Semaphore next;
  23.  
  24.     public static void init() {
  25.         o = new Semaphore(3);
  26.         h = new Semaphore(3);
  27.         al = new Semaphore(1);
  28.  
  29.         oHere = new Semaphore(0);
  30.         ohHere = new Semaphore(0);
  31.  
  32.         ohReady = new Semaphore(0);
  33.         ready = new Semaphore(0);
  34.  
  35.         done = new Semaphore(0);
  36.         next = new Semaphore(0);
  37.  
  38.     }
  39.  
  40.     public static class Hydrogen extends TemplateThread {
  41.  
  42.         public Hydrogen(int numRuns) {
  43.             super(numRuns);
  44.         }
  45.  
  46.         @Override
  47.         public void execute() throws InterruptedException {
  48.             h.acquire();
  49.             oHere.acquire();
  50.             ohReady.release();
  51.             state.bondOH();
  52.             ohHere.release();
  53.             ready.acquire();
  54.             state.bondAlOH3();
  55.             done.release();
  56.             next.acquire();
  57.             h.release();
  58.         }
  59.  
  60.     }
  61.  
  62.     public static class Oxygen extends TemplateThread {
  63.  
  64.         public Oxygen(int numRuns) {
  65.             super(numRuns);
  66.         }
  67.  
  68.         @Override
  69.         public void execute() throws InterruptedException {
  70.             o.acquire();
  71.             oHere.release();
  72.             ohReady.acquire();
  73.             state.bondOH();
  74.             ohHere.release();
  75.             ready.acquire();
  76.             state.bondAlOH3();
  77.             done.release();
  78.             next.acquire();
  79.             o.release();
  80.         }
  81.  
  82.     }
  83.  
  84.     public static class Aluminium extends TemplateThread {
  85.  
  86.         public Aluminium(int numRuns) {
  87.             super(numRuns);
  88.         }
  89.  
  90.         @Override
  91.         public void execute() throws InterruptedException {
  92.             al.acquire();
  93.             ohHere.acquire(6);
  94.             ready.release(6);
  95.             state.bondAlOH3();
  96.             done.acquire(6);
  97.             next.release(6);
  98.             state.validate();
  99.             al.release();
  100.         }
  101.  
  102.     }
  103.  
  104.     static AluminiumHydroxideState state = new AluminiumHydroxideState();
  105.  
  106.     public static void main(String[] args) {
  107.         for (int i = 0; i < 10; i++) {
  108.             run();
  109.         }
  110.     }
  111.  
  112.     public static void run() {
  113.         try {
  114.             int numRuns = 1;
  115.             int numScenarios = 300;
  116.  
  117.             HashSet<Thread> threads = new HashSet<Thread>();
  118.  
  119.             for (int i = 0; i < numScenarios; i++) {
  120.                 Oxygen o = new Oxygen(numRuns);
  121.                 Hydrogen h = new Hydrogen(numRuns);
  122.                 threads.add(o);
  123.                 if (i % 3 == 0) {
  124.                     Aluminium al = new Aluminium(numRuns);
  125.                     threads.add(al);
  126.                 }
  127.                 threads.add(h);
  128.             }
  129.  
  130.             init();
  131.  
  132.             ProblemExecution.start(threads, state);
  133.             System.out.println(new Date().getTime());
  134.         } catch (Exception ex) {
  135.             ex.printStackTrace();
  136.         }
  137.     }
  138.  
  139. }
Add Comment
Please, Sign In to add comment