Java Thread Callable executeUntilNonNullOrTimeout(final long timeoutMs, final long timeBetweenPollsMs, final Callable callable)

Here you can find the source of executeUntilNonNullOrTimeout(final long timeoutMs, final long timeBetweenPollsMs, final Callable callable)

Description

execute Until Non Null Or Timeout

License

Open Source License

Declaration

public static <V> V executeUntilNonNullOrTimeout(final long timeoutMs, final long timeBetweenPollsMs,
            final Callable<V> callable) throws InterruptedException 

Method Source Code


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

import java.util.Date;

import java.util.concurrent.Callable;

public class Main {
    public static <V> V executeUntilNonNullOrTimeout(final long timeoutMs, final long timeBetweenPollsMs,
            final Callable<V> callable) throws InterruptedException {
        final long started = new Date().getTime();
        long elapsedTime = 0;
        while (elapsedTime < timeoutMs) {
            final V value;
            try {
                value = callable.call();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }//from   www  .  j a  v  a 2 s . c om
            if (value != null) {
                return value;
            } else {
                elapsedTime = (new Date().getTime()) - started;
            }
            Thread.sleep(timeBetweenPollsMs);
        }
        return null;
    }
}

Related

  1. callInLocale(Locale locale, Callable task)
  2. callWithSystemProperty(String name, String value, Callable callee)
  3. copyFromList(final List list, final double[][] data, final int cols)
  4. copyInto(final List list, final double[] data)
  5. executeLocally(Callable task)
  6. executeWithClassLoader(ClassLoader classLoader, Callable callable)
  7. expectException(Callable callable, Class exception)
  8. getBytes(final String string)
  9. getFuture(ExecutorService executorService, Callable callable)