Example usage for java.lang Object wait

List of usage examples for java.lang Object wait

Introduction

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

Prototype

public final native void wait(long timeoutMillis) throws InterruptedException;

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

Usage

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

/**
 * ?sleep?object.wait//from ww w  . j a  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;
        }
    }
}

From source file:org.apache.struts2.interceptor.ScopeInterceptor.java

static void lock(Object o, ActionInvocation invocation) throws Exception {
    synchronized (o) {
        int count = 3;
        Object previous;/*from www .ja v a  2 s .  co  m*/
        while ((previous = locks.get(o)) != null) {
            if (previous == invocation) {
                return;
            }
            if (count-- <= 0) {
                locks.remove(o);
                o.notify();

                throw new StrutsException("Deadlock in session lock");
            }
            o.wait(10000);
        }
        locks.put(o, invocation);
    }
}

From source file:com.duker.mygift.struts.interceptor.ScopeInterceptor.java

static final void lock(Object o, ActionInvocation invocation) throws Exception {
    synchronized (o) {
        int count = 3;
        Object previous = null;/*from   w  w w  .j a  va 2  s  .  com*/
        while ((previous = locks.get(o)) != null) {
            if (previous == invocation) {
                return;
            }
            if (count-- <= 0) {
                locks.remove(o);
                o.notify();

                throw new StrutsException("Deadlock in session lock");
            }
            o.wait(10000);
        }
        locks.put(o, invocation);
    }
}

From source file:sample.action.process.ProcessAction.java

public ActionForward doRunner() throws Exception {
    log.debug("runner");

    Object obj = new Object();
    synchronized (obj) {
        log.debug("start");
        obj.wait(30000);
        log.debug("ende");
    }/*www  . j  a v  a  2 s .c  o m*/

    return ActionHelper.findForward("view");
}

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 ww w .  j a  v a2 s  .c  o m
 * @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:to.sven.androidrccar.arduino.test.AccessoryTestSuiteRunner.java

/**
 * Waits some milliseconds/*from w  w w  .  j a v  a2 s . c  o  m*/
 * @param millisec The milliseconds to wait.
 * @throws InterruptedException Someone interrupted us
 */
private void waitForAccessory(long millisec) throws InterruptedException {
    Object monitor = new Object();
    synchronized (monitor) {
        monitor.wait(millisec);
    }
}

From source file:org.cloudgraph.examples.wordnet.WordnetTestCase.java

protected void waitForMillis(long time) {
    Object lock = new Object();
    synchronized (lock) {
        try {/*  w  w  w  .  j  a v  a2 s.c  om*/
            log.info("waiting " + time + " millis...");
            lock.wait(time);
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        }
    }
    log.info("...continue");
}

From source file:org.qedeq.base.io.IoUtility.java

/**
 * Sleep my little class./*from w w w. j a  v a2 s.c o  m*/
 *
 * @param   ms  Milliseconds to wait.
 */
public static void sleep(final int ms) {
    final Object monitor = new Object();
    synchronized (monitor) {
        try {
            monitor.wait(ms);
        } catch (InterruptedException e) {
        }
    }
}

From source file:core.sample.pool.PoolTester.java

/**
 * runWithNotify - method simulates the use of wait() and notify().
 *///from   w  w w.  jav a 2s  .  c  o  m
public void runWithNotify() {
    try {
        log.debug("**** Running run().. ");
        WorkerThread rt1 = (WorkerThread) pool.borrowObject();

        Object synObj = new Object();
        rt1.execute("core.sample.pool.SampleWork", "executeTask", null, null, synObj);
        synchronized (synObj) {
            synObj.wait(4000);
        }
        Object result = rt1.getResult();
        log.debug("**Result = " + result);

        pool.returnObject(rt1);
    } catch (Exception e) {
        log.error("", e);
    }
}

From source file:org.apache.cxf.dosgi.systests.common.rest.AbstractListenerHookServiceListenerTest.java

private void verifyGreeterResponse(FutureTask<GreeterInfo> task, Object mutex) throws Exception {
    GreeterInfo greetings = null;/*from www . j  a  va2 s  . co m*/
    synchronized (mutex) {
        while (task == null) {
            mutex.wait(500);
        }
        greetings = task.get();
    }

    assertEquals("4 greetings expected", 4, greetings.getGreetings().size());
}