Example usage for java.util.concurrent Callable call

List of usage examples for java.util.concurrent Callable call

Introduction

In this page you can find the example usage for java.util.concurrent Callable call.

Prototype

V call() throws Exception;

Source Link

Document

Computes a result, or throws an exception if unable to do so.

Usage

From source file:com.aerospike.delivery.db.base.Database.java

public static boolean withWriteLock(ReentrantReadWriteLock lock, Callable<Boolean> action) {
    lock.writeLock().lock();/*from ww w .jav  a  2  s .  com*/
    try {
        return action.call();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        lock.writeLock().unlock();
    }
}

From source file:com.cedarsoft.serialization.SplittingPerformanceRunner.java

private static void run(@Nonnull String description, @Nonnull Callable<String> callable) throws Exception {
    //Warmup//from  ww w  .  j  a v a  2s. co  m
    for (int i = 0; i < 1000; i++) {
        assertEquals("1.0.0", callable.call());
    }

    //Do the work
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    for (int i = 0; i < 100000; i++) {
        assertEquals("1.0.0", callable.call());
    }

    stopWatch.stop();
    System.out.println(description + " took " + stopWatch.getTime());
}

From source file:com.sf.ddao.chain.CtxHelper.java

public static <T> T get(Context ctx, Class<T> clazz, Callable<T> callback) throws Exception {
    //noinspection unchecked
    T res = (T) ctx.get(clazz.toString());
    if (res == null) {
        res = callback.call();
        put(ctx, clazz, res);/*from  w  ww  .j av a2  s . co  m*/
    }
    return res;
}

From source file:org.libreplan.web.resourceload.ResourceLoadParameters.java

private static <T> T call(Callable<T> all) {
    try {/*w w  w.  ja  v  a 2s  .  c o  m*/
        return all.call();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:OSGiLocator.java

/**
 * Finds all providers for the given service.
 * /*from w  w w .j av a 2s.c o m*/
 * @param serviceName
 *          The fully qualified name of the service interface.
 * @return The ordered set of providers for the service if any exists.
 *         Otherwise, it returns an empty list.
 * @throws IllegalArgumentException
 *           if serviceName is <tt>null</tt>
 */
public static synchronized List<Class<?>> locateAll(final String serviceName) {
    if (serviceName == null) {
        throw new IllegalArgumentException("serviceName cannot be null");
    }
    List<Class<?>> classes = new ArrayList<Class<?>>();
    if (factories != null) {
        List<Callable<Class<?>>> l = factories.get(serviceName);
        if (l != null) {
            for (Callable<Class<?>> c : l) {
                try {
                    classes.add(c.call());
                } catch (Exception e) {
                }
            }
        }
    }
    return classes;
}

From source file:OSGiLocator.java

/**
 * Finds the preferred provider for the given service. The preferred provider
 * is the last one added to the set of providers.
 * /*from www  .jav a  2s.c  o m*/
 * @param serviceName
 *          The fully qualified name of the service interface.
 * @return The last provider added for the service if any exists. Otherwise,
 *         it returns <tt>null</tt>.
 * @throws IllegalArgumentException
 *           if serviceName is <tt>null</tt>
 */
public static synchronized Class<?> locate(final String serviceName) {
    if (serviceName == null) {
        throw new IllegalArgumentException("serviceName cannot be null");
    }
    if (factories != null) {
        List<Callable<Class<?>>> l = factories.get(serviceName);
        if (l != null && !l.isEmpty()) {
            Callable<Class<?>> c = l.get(l.size() - 1);
            try {
                return c.call();
            } catch (Exception e) {
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Invoke the specified <code>Callable</code> on the AWT event dispatching thread now and return
 * the result.<br>//from   www .j a va 2 s .  c  om
 * The returned result can be <code>null</code> when a {@link Throwable} exception happen.<br>
 * Use this method carefully as it may lead to dead lock.
 * 
 * @throws InterruptedException
 *         if the current thread was interrupted while waiting
 * @throws Exception
 *         if the computation threw an exception
 */
public static <T> T invokeNow(Callable<T> callable) throws InterruptedException, Exception {
    if (SwingUtilities.isEventDispatchThread())
        return callable.call();

    final FutureTask<T> task = new FutureTask<T>(callable);

    try {
        EventQueue.invokeAndWait(task);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof Exception)
            throw (Exception) e.getCause();

        // not an exception --> handle it
        //IcyExceptionHandler.showErrorMessage(e, true);
        return null;
    }

    try {
        return task.get();
    } catch (ExecutionException e) {
        if (e.getCause() instanceof Exception)
            throw (Exception) e.getCause();

        // not an exception --> handle it
        //IcyExceptionHandler.showErrorMessage(e, true);
        return null;
    }
}

From source file:com.api6.zkclient.util.TestUtil.java

/**
 * callableexpectedValue/* w w w  .  jav a2 s . com*/
 * @param expectedValue
 * @param callable
 * @param timeUnit
 * @param timeout
 * @throws Exception 
 * @return T callable
 */
public static <T> T waitUntil(T expectedValue, Callable<T> callable, TimeUnit timeUnit, long timeout)
        throws Exception {
    long startTime = System.currentTimeMillis();
    do {
        T actual = callable.call();
        if (expectedValue.equals(actual)) {
            System.out.println("TestUtil.waitUntil expected");
            return actual;
        }
        if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) {
            System.out.println("TestUtil.waitUntil timeout!");
            return actual;
        }
        Thread.sleep(300);
    } while (true);
}

From source file:org.wso2.das.integration.common.utils.Utils.java

private static boolean checkResultOrException(Callable<Boolean> exec) {
    try {/*w ww  . j av  a  2 s. c om*/
        return exec.call();
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Check Result Or Exception: " + e.getMessage());
        }
        return false;
    }
}

From source file:org.zanata.magpie.api.service.impl.AccountResourceImpl.java

private static <T> T tryApplyChange(Callable<T> callable) {
    try {/*from w  ww  .ja  va  2s . c  o m*/
        return callable.call();
    } catch (Exception e) {
        Optional<Throwable> anyConstraintViolation = Throwables.getCausalChain(e).stream()
                .filter(t -> t instanceof ConstraintViolationException).findAny();
        if (anyConstraintViolation.isPresent()) {
            throw new DataConstraintViolationException(Throwables.getRootCause(e));
        }
        throw new MTException("error registering new account", e);
    }
}