Semaphores

mail@pastecode.io avatar
unknown
java
a year ago
633 B
2
Indexable
Never
class H2O {

    Semaphore HSema = new Semaphore(2);
    Semaphore OSema = new Semaphore(0);

    public H2O() {
        
    }

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

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