Advertisement
Guest User

H2O Machine

a guest
Apr 9th, 2020
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.concurrent.Semaphore;
  3.  
  4. class H2OMachine {
  5.  
  6.     String[] molecule;
  7.     int count;
  8.  
  9.     public H2OMachine() {
  10.         molecule = new String[3];
  11.         count = 0;
  12.     }
  13.  
  14.     static Semaphore hydrogen = new Semaphore(1);
  15.     static Semaphore oxygen = new Semaphore(1);
  16.     static Semaphore hHere = new Semaphore(0);
  17.     static Semaphore countLock = new Semaphore(1);
  18.  
  19.     public void hydrogen() throws InterruptedException {
  20.         // TODO: 3/29/20 synchronized logic here
  21.         hydrogen.acquire();
  22.         countLock.acquire();
  23.         molecule[count++]="H";
  24.         if(count==3){
  25.             System.out.println("Hydrogen atom 2 arrived");
  26.             System.out.println(String.join("-", molecule));
  27.             count=0;
  28.             countLock.release();
  29.             hydrogen.release();
  30.             oxygen.release();
  31.             return;
  32.         }
  33.         System.out.println("Hydrogen atom 1 arrived");
  34.         hHere.release();
  35.         countLock.release();
  36.     }
  37.  
  38.     public void oxygen() throws InterruptedException {
  39.         // TODO: 3/29/20 synchronized logic here
  40.         oxygen.acquire();
  41.         hHere.acquire();
  42.         countLock.acquire();
  43.         System.out.println("Oxygen arrived");
  44.         molecule[count++]="O";
  45.         countLock.release();
  46.         hydrogen.release();
  47.     }
  48. }
  49.  
  50. class H2OThread extends Thread {
  51.  
  52.     H2OMachine molecule;
  53.     String atom;
  54.  
  55.     public H2OThread(H2OMachine molecule, String atom) {
  56.         this.molecule = molecule;
  57.         this.atom = atom;
  58.     }
  59.  
  60.     public void run() {
  61.         if ("H".equals(atom)) {
  62.             try {
  63.                 molecule.hydrogen();
  64.             } catch (Exception e) {
  65.             }
  66.         } else if ("O".equals(atom)) {
  67.             try {
  68.                 molecule.oxygen();
  69.             } catch (Exception e) {
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75. public class Main {
  76.     public static void main(String[] args) throws InterruptedException {
  77.  
  78.         // TODO: 3/29/20 Simulate with multiple scenarios
  79.         H2OMachine molecule = new H2OMachine();
  80.         int n = 120;
  81.         H2OThread[] threads = new H2OThread[n];
  82.         for (int i = 0; i < n; i++) {
  83.             if (i%3==0) {
  84.                 threads[i] = new H2OThread(molecule, "O");
  85.             } else threads[i] = new H2OThread(molecule, "H");
  86.         }
  87.         Arrays.stream(threads).forEach(Thread::start);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement