Example usage for java.util.concurrent FutureTask get

List of usage examples for java.util.concurrent FutureTask get

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask get.

Prototype

public V get() throws InterruptedException, ExecutionException 

Source Link

Usage

From source file:Main.java

public static <T> T invokeAndWait(Callable<T> callable)
        throws TimeoutException, ExecutionException, InterruptedException {
    //blocks until future returns
    FutureTask<T> task = new FutureTask<>(callable);
    SwingUtilities.invokeLater(task);
    return task.get();
}

From source file:Main.java

public static void runInNewThread(String threadName, Runnable target) throws Throwable {
    FutureTask<Object> future = new FutureTask<Object>(target, null);
    new Thread(future, threadName).start();
    try {//from  www  . j  a v a 2  s.  c  om
        future.get();
    } catch (ExecutionException e) {
        throw e.getCause();
    }
}

From source file:Main.java

/**
 * Run the supplied Callable on the main thread, The method will block until the Callable
 * completes./*from  w  w w. j  a  va2s . co m*/
 *
 * @param c The Callable to run
 * @return The result of the callable
 * @throws ExecutionException c's exception
 */
public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
    FutureTask<T> task = new FutureTask<T>(c);
    runOnUiThread(task);
    try {
        return task.get();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted waiting for callable", e);
    }
}

From source file:Main.java

/**
 * Run the supplied Runnable on the main thread. The method will block until the Runnable
 * completes.//from w  ww .j a  v a 2s .  c om
 *
 * @param r The Runnable to run.
 */
public static void runOnUiThreadBlocking(final Runnable r) {
    if (runningOnUiThread()) {
        r.run();
    } else {
        FutureTask<Void> task = new FutureTask<Void>(r, null);
        postOnUiThread(task);
        try {
            task.get();
        } catch (Exception e) {
            throw new RuntimeException("Exception occured while waiting for runnable", e);
        }
    }
}

From source file:org.eclipse.jubula.rc.javafx.driver.EventThreadQueuerJavaFXImpl.java

/**
 * Executes the given Callable on the JavaFX-Thread and waits for the
 * termination//from w w  w .  j av  a2 s . co m
 *
 * @param name
 *            a name to identifier which Callable is being executed
 * @param <V>
 *            return value type
 * @param call
 *            the Callable
 * @return
 * @return the return value of the given Callable
 * @throws ExecutionException
 * @throws InterruptedException
 */
public static <V> V invokeAndWait(String name, Callable<V> call) {

    if (Platform.isFxApplicationThread()) {
        try {
            return call.call();
        } catch (Exception e) {
            // the run() method from IRunnable has thrown an exception
            // -> log on info
            // -> throw a StepExecutionException
            Throwable thrown = e.getCause();
            if (thrown instanceof StepExecutionException) {
                if (log.isInfoEnabled()) {
                    log.info(e);
                }
                throw (StepExecutionException) thrown;
            }

            // any other (unchecked) Exception from IRunnable.run()
            log.error("exception thrown by '" + name //$NON-NLS-1$
                    + "':", thrown); //$NON-NLS-1$
            throw new StepExecutionException(thrown);
        }
    }
    try {
        FutureTask<V> task = new FutureTask<>(call);
        Platform.runLater(task);
        return task.get();

    } catch (InterruptedException ie) {
        // this (the waiting) thread was interrupted -> error
        log.error(ie);
        throw new StepExecutionException(ie);
    } catch (ExecutionException ee) {
        // the run() method from IRunnable has thrown an exception
        // -> log on info
        // -> throw a StepExecutionException
        Throwable thrown = ee.getCause();
        if (thrown instanceof StepExecutionException) {
            if (log.isInfoEnabled()) {
                log.info(ee);
            }
            throw (StepExecutionException) thrown;
        }

        // any other (unchecked) Exception from IRunnable.run()
        log.error("exception thrown by '" + name //$NON-NLS-1$
                + "':", thrown); //$NON-NLS-1$
        throw new StepExecutionException(thrown);
    }
}

From source file:com.googlecode.aviator.AviatorEvaluator.java

private static Expression getCompiledExpression(final String expression, FutureTask<Expression> task) {
    try {//from w  w w.j  ava  2 s . co m
        return task.get();
    } catch (Exception e) {
        cacheExpressions.remove(expression);
        throw new CompileExpressionErrorException("Compile expression failure:" + expression, e);
    }
}

From source file:org.apache.jackrabbit.oak.jcr.CompatibilityIssuesTest.java

private static void run(Callable<Void> callable) throws InterruptedException, ExecutionException {
    FutureTask<Void> task = new FutureTask<Void>(callable);
    new Thread(task).start();
    task.get();
}

From source file:Main.java

/**
 * Invoke the specified <code>Callable</code> on the AWT event dispatching thread now and return
 * the result.<br>//from  w  w w.j a  v  a 2 s  .c  o  m
 * 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:org.apache.jackrabbit.oak.plugins.segment.CompactionAndCleanupIT.java

private static <T> T run(Callable<T> callable) throws InterruptedException, ExecutionException {
    FutureTask<T> task = new FutureTask<T>(callable);
    new Thread(task).start();
    return task.get();
}

From source file:net.minecraftforge.fml.common.FMLCommonHandler.java

public static void callFuture(FutureTask task) {
    try {/*from w w w  . j a  va  2  s .c  o  m*/
        task.run();
        task.get(); // Forces the exception to be thrown if any
    } catch (InterruptedException e) {
        FMLLog.log(Level.FATAL, e, "Exception caught executing FutureTask: " + e.toString());
    } catch (ExecutionException e) {
        FMLLog.log(Level.FATAL, e, "Exception caught executing FutureTask: " + e.toString());
    }
}