LeetCode-in-Java

1117. Building H2O

Medium

There are two kinds of threads: oxygen and hydrogen. Your goal is to group these threads to form water molecules.

There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given releaseHydrogen and releaseOxygen methods respectively, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.

In other words:

We do not have to worry about matching the threads up explicitly; the threads do not necessarily know which other threads they are paired up with. The key is that threads pass the barriers in complete sets; thus, if we examine the sequence of threads that bind and divide them into groups of three, each group should contain one oxygen and two hydrogen threads.

Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

Example 1:

Input: water = “HOH”

Output: “HHO”

Explanation: “HOH” and “OHH” are also valid answers.

Example 2:

Input: water = “OOHHHH”

Output: “HHOHHO”

Explanation: “HOHHHO”, “OHHHHO”, “HHOHOH”, “HOHHOH”, “OHHHOH”, “HHOOHH”, “HOHOHH” and “OHHOHH” are also valid answers.

Constraints:

Solution

import java.util.concurrent.Semaphore;

@SuppressWarnings({"java:S106", "java:S2142"})
public class H2O {
    private Semaphore hSemaphore;
    private Semaphore oSemaphore;

    public H2O() {
        hSemaphore = new Semaphore(2);
        oSemaphore = new Semaphore(2);
    }

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

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

    public static class HydrogenRunnable implements Runnable {
        private H2O h2O;

        public HydrogenRunnable(H2O h2O) {
            this.h2O = h2O;
        }

        @Override
        public void run() {
            try {
                h2O.hydrogen(() -> System.out.print('H'));
            } catch (InterruptedException ignored) {
                // ignored
            }
        }
    }

    public static class OxygenRunnable implements Runnable {
        private H2O h2O;

        public OxygenRunnable(H2O h2O) {
            this.h2O = h2O;
        }

        @Override
        public void run() {
            try {
                h2O.oxygen(() -> System.out.print('O'));
            } catch (InterruptedException ignored) {
                // ignored
            }
        }
    }
}