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:Main.java

public static void wait(Object o, int timeout) {
    synchronized (o) {
        while (true) {
            try {
                o.wait(timeout);
                return;
            } catch (InterruptedException e) {
            }/*ww  w .  j ava 2 s .  co m*/
        }
    }
}

From source file:Main.java

/**
 * Wait until another thread calls notify on the given mutex or the timeout
 * expires//from w  ww . j  ava 2  s .  com
 * 
 * @param timeout
 * @param connectionTimeout
 */
public static void mutexWait(Object mutex, long timeout) {
    synchronized (mutex) {
        waitLoop: while (true)
            try {
                mutex.wait(timeout);
                break waitLoop;
            } catch (InterruptedException e) {
                continue waitLoop;
            }
    }
}

From source file:Main.java

/**
 * Wait on the given object for the specified number of milliseconds.
 * This method will return the actual amount of time that was waited.
 * The current thread must hold the lock on the given object. 
 *//*from   w w w.  j  ava 2  s.  c om*/
public static long wait(Object o, long timeout) {
    long start = System.currentTimeMillis();
    if (Thread.holdsLock(o)) {
        try {
            o.wait(timeout);
        } catch (InterruptedException e) {
        }
    }
    return System.currentTimeMillis() - start;
}

From source file:Main.java

/**
 * If the given time (based on {@link System#currentTimeMillis}) has passed, throw a TimeoutException.
 * Otherwise, invoke {@link Object#wait(long)} on the given object, which may return due to
 * a {@code notify()} call, the timeout being reached, or a spurious wake-up.  To distinguish between
 * these possibilities, clients should wrap this call in a while loop:
 * {@code long t = futureTimeMillis(...); while (!condition) waitUntilMillis(lock, t);}
 * This loop either completes if the condition is satisfied or throws an appropriate exception
 * due to an interrupt or timeout./*w ww .j a v a  2s. c o m*/
 * @param obj  Object whose {@code wait()} method will be invoked.  Must be locked by the current thread.
 * @param futureTime  A millisecond time value based on {@code System.currentTimeMillis()} after which
 *                    this method should no longer invoke {@code obj.wait()}.
 * @throws InterruptedException  If the wait is interrupted.
 * @throws TimeoutException  If, at invocation time, {@code futureTime} is in the past.
 * @see #futureTimeMillis
 */
public static void waitUntilMillis(Object obj, long futureTime) throws InterruptedException, TimeoutException {
    long delta = futureTime - System.currentTimeMillis();
    if (delta > 0) {
        obj.wait(delta);
    } else {
        throw new TimeoutException();
    }
}

From source file:Main.java

/**
 * Waits for the specified object./*  w  ww.ja  v  a  2  s .  c om*/
 * More or less the same as calling <code>wait(long timeout)</code> on the object,
 * but this handles spurious wakeups. So in the absence of a notify,
 * this is guaranteed to wait for at least the specified amount of time.
 */
public static void waitTimeout(Object object, long timeout) {
    long endTime = System.currentTimeMillis() + timeout;
    boolean interrupted = true;
    while (interrupted) {
        interrupted = false;
        try {
            object.wait(endTime - System.currentTimeMillis());
        } catch (InterruptedException ex) {
            interrupted = true;
        }
    }
}

From source file:czlab.xlib.CU.java

public static void blockAndWait(Object lock, long waitMillis) {
    try {/*ww  w.j  a v  a2 s  .  c o m*/
        synchronized (lock) {
            if (waitMillis > 0L) {
                lock.wait(waitMillis);
            } else {
                lock.wait();
            }
        }
    } catch (Throwable e) {
        TLOG.error("", e);
    }
}

From source file:io.insideout.stanbol.enhancer.nlp.freeling.TestFreelingAnalysis.java

@AfterClass
public static final void cleanUp() {
    freeling.close();/*  www.ja va  2  s. com*/
    Assert.assertTrue(freeling.isClosed());
    Assert.assertTrue(freeling.getSupportedLanguages().isEmpty());
    Assert.assertFalse(freeling.isLanguageIdentificationSupported());
    try {
        Object o = new Object();
        synchronized (o) {
            o.wait(10000);
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.beetle.framework.util.OtherUtil.java

/**
 * ?sleep?object.wait/*from w w  w. ja  va  2 s .  c o  m*/
 * 
 * @param lockObj
 *            ?
 * @param sometime
 *            ??
 * @throws InterruptedException
 */
public static final void blockSomeTime(final Object lockObj, long sometime) throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
    synchronized (lockObj) {
        long waitTime = sometime;
        long start = System.currentTimeMillis();
        try {
            for (;;) {
                lockObj.wait(waitTime);
                waitTime = sometime - (System.currentTimeMillis() - start);
                if (waitTime <= 0) {
                    break;
                }
            }
        } catch (InterruptedException ex) {
            lockObj.notify();
            throw ex;
        }
    }
}

From source file:org.eclipse.orion.internal.server.servlets.task.TaskJobHandler.java

/**
 * Schedules {@link TaskJob} and handles response. If job lasts more than {@link this#WAIT_TIME}
 * handler starts a task and returns 202 (Accepted) response with task details. If job finishes sooner the
 * response if immediately returned filled with {@link TaskJob#getResult()}, if result is OK, than only
 * {@link ServerStatus#getJsonData()} is returned, if result is OK and it is not an instance of {@link ServerStatus}
 * than {@link TaskJob#getFinalResult()} is returned as a response content.
 * /*from  ww  w.  j  av  a 2  s  . c o m*/
 * @param request
 * @param response
 * @param job Job that should be handled as a task.
 * @param statusHandler status handler to handle error statuses.
 * @return <code>true</code> if job was handled properly.
 * @throws IOException
 * @throws ServletException
 * @throws URISyntaxException
 * @throws JSONException
 */
public static boolean handleTaskJob(HttpServletRequest request, HttpServletResponse response, TaskJob job,
        ServletResourceHandler<IStatus> statusHandler)
        throws IOException, ServletException, URISyntaxException, JSONException {
    job.schedule();

    final Object jobIsDone = new Object();
    final JobChangeAdapter jobListener = new JobChangeAdapter() {
        public void done(IJobChangeEvent event) {
            synchronized (jobIsDone) {
                jobIsDone.notify();
            }
        }
    };
    job.addJobChangeListener(jobListener);

    try {
        synchronized (jobIsDone) {
            if (job.getState() != Job.NONE) {
                jobIsDone.wait(WAIT_TIME);
            }
        }
    } catch (InterruptedException e) {
    }
    job.removeJobChangeListener(jobListener);

    if (job.getState() == Job.NONE || job.getRealResult() != null) {
        return writeResult(request, response, job, statusHandler);
    } else {
        TaskInfo task = job.startTask();
        JSONObject result = task.toJSON();
        URI taskLocation = createTaskLocation(ServletResourceHandler.getURI(request), task.getId(),
                task.isKeep());
        result.put(ProtocolConstants.KEY_LOCATION, taskLocation);
        if (!task.isRunning()) {
            job.removeTask(); // Task is not used, we may remove it
            return writeResult(request, response, job, statusHandler);
        }
        response.setHeader(ProtocolConstants.HEADER_LOCATION,
                ServletResourceHandler.resovleOrionURI(request, taskLocation).toString());
        OrionServlet.writeJSONResponse(request, response, result);
        response.setStatus(HttpServletResponse.SC_ACCEPTED);
        return true;
    }
}

From source file:gov.nasa.ensemble.common.CommonUtils.java

/**
 * Like Object.wait(long) except without the necessity of surrounding with try/catch
 *///from www  .j  a va  2  s.  c  o m
public static void wait(Object object, long milliseconds) {
    try {
        object.wait(milliseconds);
    } catch (InterruptedException e) {
        // do nothing
    }
}