Example usage for java.lang Object notifyAll

List of usage examples for java.lang Object notifyAll

Introduction

In this page you can find the example usage for java.lang Object notifyAll.

Prototype

@HotSpotIntrinsicCandidate
public final native void notifyAll();

Source Link

Document

Wakes up all threads that are waiting on this object's monitor.

Usage

From source file:org.echocat.jconscius.cluster.impl.relay.station.RelayStationLauncher.java

public static void main(String[] arguments) throws Exception {
    final RelayStation station = createStation(arguments);
    if (station != null) {
        final Object lock = new Object();
        final Thread shutdownThread = new Thread("ShutdownHook") {
            @Override//  w  w w.  j  ava2 s  .co m
            public void run() {
                LOG.info("Station is going down now...");
                station.close();
                LOG.info("Station is down! Bye.");
                synchronized (lock) {
                    lock.notifyAll();
                }
            }
        };
        Runtime.getRuntime().addShutdownHook(shutdownThread);
        LOG.info("Station is starting...");
        station.init();
        LOG.info("Station is ready!");

        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (lock) {
            lock.wait();
        }
    }
}

From source file:ThreadState.java

public static void main(String[] args) throws InterruptedException {
    Object syncObject = new Object();
    ThreadState ts = new ThreadState(syncObject);
    System.out.println("Before start()-ts.isAlive():" + ts.isAlive());
    System.out.println("#1:" + ts.getState());

    ts.start();/*from ww w . j  ava  2s .  c  o m*/
    System.out.println("After start()-ts.isAlive():" + ts.isAlive());
    System.out.println("#2:" + ts.getState());
    ts.setWait(true);

    Thread.currentThread().sleep(100);

    synchronized (syncObject) {
        System.out.println("#3:" + ts.getState());
        ts.setWait(false);
        syncObject.notifyAll();
    }

    Thread.currentThread().sleep(2000);
    System.out.println("#4:" + ts.getState());
    ts.setKeepRunning(false);

    Thread.currentThread().sleep(2000);
    System.out.println("#5:" + ts.getState());
    System.out.println("At the   end. ts.isAlive():" + ts.isAlive());
}

From source file:Main.java

public static void notifyAll(Object o) {
    synchronized (o) {
        o.notifyAll();
    }
}

From source file:Main.java

/**
 * Notifies an object for synchronization purposes.
 *///from  w ww. j  a v a2 s  .co  m
public static void notifyAll(Object obj) {
    synchronized (obj) {
        obj.notifyAll();
    }
}

From source file:Main.java

public static final void doNotifyAll(Object obj) {
    synchronized (obj) {
        obj.notifyAll();
    }// ww w. j ava2s . co  m
}

From source file:Main.java

public static void doNotifyAll(Object syncObj) {
    synchronized (syncObj) {
        syncObj.notifyAll();
    }
}

From source file:Main.java

/**
 * Notifies all threads that wait for the given mutex
 * //  w  ww .  ja  va 2 s.  c  o  m
 * @param mutex
 */
public static void mutexNotifyAll(Object mutex) {
    synchronized (mutex) {
        mutex.notifyAll();
    }
}

From source file:czlab.xlib.CU.java

public static void unblock(Object lock) {
    try {//w w w. j  av  a2s.c  o m
        synchronized (lock) {
            lock.notifyAll();
        }
    } catch (Throwable e) {
        TLOG.error("", e);
    }
}

From source file:org.nebulaframework.discovery.multicast.MulticastDiscovery.java

/**
 * Invoked by GridNodes to detect Clusters with in multicast
 * reachable network range. The discovery process returns 
 * without results if no cluster was discovered with in 
 * a fixed duration, set by {@link #TIMEOUT}.
 * //from  w  w  w .java2s  .  com
 * @return IP Address and Port of detected Cluster (String)
 */
public static String discoverCluster() {

    log.debug("[MulticastDiscovery] Attempting to discover cluster");

    // Synchronization Mutex
    final Object mutex = new Object();

    // Create Instance of MulticastDiscovery
    final MulticastDiscovery mDisc = new MulticastDiscovery();

    // Execute on a separate Thread
    new Thread(new Runnable() {

        public void run() {
            try {
                // Attempt Discovery
                mDisc.doDiscover();
            } catch (IOException e) {
                log.debug("[MulticastDiscovery] Failed to Discover", e);
            }

            // Notify waiting Threads
            synchronized (mutex) {
                mutex.notifyAll();
            }

        }

    }).start();

    // Wait till response come or timeout happens
    synchronized (mutex) {

        try {
            mutex.wait(TIMEOUT);
        } catch (InterruptedException e) {
            // Should not happen
            throw new AssertionError(e);
        }

    }

    if (mDisc.cluster != null) {
        log.info("[MulticastDiscovery] Discovered Cluster at " + mDisc.cluster);
    }
    return mDisc.cluster;
}

From source file:com.titilink.camel.rest.util.OtherUtil.java

/**
 * ?sleep?object.wait//from   w w w.  ja v  a 2  s.  c  o  m
 *
 * @param lockObj  ?
 * @param sometime ??
 * @throws InterruptedException --
 */
public static void blockSomeTime(final Object lockObj, long sometime) throws InterruptedException {
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }

    synchronized (lockObj) {
        long waitTime = sometime;
        long start = OtherUtil.getCurrentTime();
        try {
            for (;;) {
                if (waitTime > 0) {
                    lockObj.wait(waitTime);
                } else {
                    break;
                }

                waitTime = sometime - (OtherUtil.getCurrentTime() - start);
            }
        } catch (InterruptedException ex) {
            lockObj.notifyAll();
            throw ex;
        }
    }
}