@@ -0,0 +1,2442 @@
+package leetcode.concurrency;%0A%0Aimport java.util.concurrent.locks.Condition;%0Aimport java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReentrantLock;%0A%0Apublic class H2O %7B%0A%0A    private int amountOfHydrogens = 0;%0A    private Lock lock = new ReentrantLock();%0A    private Condition oxygenFinished = lock.newCondition();%0A    private Condition hydrogenFinished = lock.newCondition();%0A%0A    public H2O() %7B%0A    %7D%0A%0A    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException %7B%0A        lock.lock();%0A        try %7B%0A            while (amountOfHydrogens == 2) %7B%0A                oxygenFinished.await();%0A            %7D%0A            releaseHydrogen.run();%0A            amountOfHydrogens++;%0A            hydrogenFinished.signalAll();%0A        %7D finally %7B%0A            lock.unlock();%0A        %7D%0A    %7D%0A%0A    public void oxygen(Runnable releaseOxygen) throws InterruptedException %7B%0A        lock.lock();%0A        try %7B%0A            while (amountOfHydrogens %3C 2) %7B%0A                hydrogenFinished.await();%0A            %7D%0A            releaseOxygen.run();%0A            amountOfHydrogens = 0;%0A            oxygenFinished.signalAll();%0A        %7D finally %7B%0A            lock.unlock();%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws InterruptedException %7B%0A        H2O h2O = new H2O();%0A        Thread hydrogen1 = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    h2O.hydrogen(() -%3E System.out.print(%22H%22));%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);%0A        Thread hydrogen2 = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    h2O.hydrogen(() -%3E System.out.print(%22H%22));%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);%0A        Thread oxygen = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    h2O.oxygen(() -%3E System.out.print(%22O%22));%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);%0A%0A        hydrogen1.start();%0A        hydrogen2.start();%0A        oxygen.start();%0A%0A        hydrogen1.join();%0A        hydrogen2.join();%0A        oxygen.join();%0A    %7D%0A%7D%0A

Add H20