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

org.enodeframework.common.threading.ManualResetEvent Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package org.enodeframework.common.threading;

import org.enodeframework.common.exception.EnodeInterruptException;

/**
 * @author [email protected]
 */
public class ManualResetEvent {

    private final Object monitor = new Object();

    private volatile boolean open = false;

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

    public boolean waitOne() {
        synchronized (monitor) {
            if (!open) {
                try {
                    monitor.wait();
                } catch (InterruptedException e) {
                    throw new EnodeInterruptException(e);
                }
            }
            return open;
        }
    }

    public boolean waitOne(long timeout) {
        synchronized (monitor) {
            if (!open) {
                try {
                    monitor.wait(timeout);
                } catch (InterruptedException e) {
                    throw new EnodeInterruptException(e);
                }
            }
            return open;
        }
    }

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

    public void reset() {
        open = false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy