Java Thread Callable waitFor(Object o, long ms, Callable c)

Here you can find the source of waitFor(Object o, long ms, Callable c)

Description

Wait for async work to finish.

License

Open Source License

Parameter

Parameter Description
o a parameter
ms a parameter
c a parameter

Exception

Parameter Description
InterruptedException an exception
Exception an exception

Declaration

public static void waitFor(Object o, long ms, Callable<Boolean> c) throws InterruptedException, Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.Callable;

public class Main {
    /**/*from ww  w .j  ava 2  s  .  c o m*/
     * Wait for async work to finish. Return when condition is true or given time has passed.
     * @param o
     * @param ms
     * @param c
     * @throws InterruptedException
     * @throws Exception
     */
    public static void waitFor(Object o, long ms, Callable<Boolean> c) throws InterruptedException, Exception {
        synchronized (o) {
            final long before = System.currentTimeMillis();
            final long step = ms / 10;
            long d = ms;
            while (!c.call() && (0 < d)) {
                o.wait(0 < step ? step : 1);
                d = ms - (System.currentTimeMillis() - before);
            }
        }
    }
}

Related

  1. submitAndWait(ListeningExecutorService service, Callable ca)
  2. submitManyAndWait(ListeningExecutorService service, Iterable> cas, FutureCallback fca)
  3. sum(final double[] a)
  4. sumOfSquares(final double[] data)
  5. waitFor(final Process process, final CyclicBarrier onStart)
  6. waitForCondition(Callable condition, Callable callback, int seconds)