package leetcode;

/**
 * https://leetcode.com/problems/building-h2o/
 */
public class Problem1117 {
    private static class H2O {
        public H2O() {
        }

        public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
            // releaseHydrogen.run() outputs "H". Do not change or remove this line.
            releaseHydrogen.run();
        }

        public void oxygen(Runnable releaseOxygen) throws InterruptedException {
            // releaseOxygen.run() outputs "O". Do not change or remove this line.
            releaseOxygen.run();
        }
    }
}

@@ -12,16 +12,56 @@
 tcode;%0A%0A
+import java.util.concurrent.Semaphore;%0A%0A
 /**%0A * h
@@ -165,16 +165,155 @@
 s H2O %7B%0A
+        private final Semaphore hSemaphore = new Semaphore(2, true);%0A        private final Semaphore oSempahore = new Semaphore(0, true);%0A%0A
         
@@ -500,32 +500,66 @@
 move this line.%0A
+            hSemaphore.acquire();%0A
             rele
@@ -569,32 +569,66 @@
 Hydrogen.run();%0A
+            oSempahore.release();%0A
         %7D%0A%0A     
@@ -802,38 +802,895 @@
     
-releaseOxygen.run();%0A        %7D
+oSempahore.acquire(2);%0A            releaseOxygen.run();%0A            hSemaphore.release(2);%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        H2O h2o = new H2O();%0A        h2o.oxygen(() -%3E System.out.print(%22O%22));%0A        Thread t1 = new Thread(() -%3E %7B%0A            try %7B%0A                h2o.hydrogen(() -%3E %7B%0A                    for (int i = 0; i %3C 5; i++) %7B%0A                        System.out.print(%22H%22);%0A                    %7D%0A                %7D);%0A            %7D catch (Exception e) %7B%7D%0A        %7D);%0A        Thread t2 = new Thread(() -%3E %7B%0A            try %7B%0A                h2o.oxygen(() -%3E %7B%0A                    for (int i = 0; i %3C 5; i++) %7B%0A                        System.out.print(%22O%22);%0A                    %7D%0A                %7D);%0A            %7D catch (Exception e) %7B%7D%0A        %7D);%0A        t1.start();%0A        t2.start();%0A        t1.join();%0A        t2.join();
 %0A   

Solve problem 1117