All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.core.ManualResetEvent Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.core;

import lombok.SneakyThrows;
import org.rx.core.exception.InvalidException;

import java.util.concurrent.TimeoutException;

import static org.rx.core.App.TIMEOUT_INFINITE;

public final class ManualResetEvent {
    private final Object monitor = new Object();
    private volatile boolean open;

    public ManualResetEvent() {
        this(false);
    }

    public ManualResetEvent(boolean initialState) {
        this.open = initialState;
    }

    @SneakyThrows
    public void waitOne() {
        waitOne(TIMEOUT_INFINITE);
    }

    public void waitOne(long timeout) throws TimeoutException {
        timeout = timeout == TIMEOUT_INFINITE ? 0 : timeout;
        synchronized (monitor) {
            while (!open) {
                try {
                    monitor.wait(timeout);
                } catch (InterruptedException e) {
                    //ignore
                    throw InvalidException.sneaky(e);
                }
                if (timeout > 0) {
                    if (!open) {
                        throw new TimeoutException("Call waitOne() time out");
                    }
                    break;
                }
            }
        }
    }

    public void set() {//open start
        synchronized (monitor) {
            open = true;
            monitor.notifyAll();
        }
    }

    public void reset() {//close stop
        open = false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy