Example usage for java.util.concurrent FutureTask FutureTask

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

Introduction

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

Prototype

public FutureTask(Callable<V> callable) 

Source Link

Document

Creates a FutureTask that will, upon running, execute the given Callable .

Usage

From source file:FutureTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): ");
    String directory = in.nextLine();
    System.out.print("Enter keyword (e.g. volatile): ");
    String keyword = in.nextLine();

    MatchCounter counter = new MatchCounter(new File(directory), keyword);
    FutureTask<Integer> task = new FutureTask<Integer>(counter);
    Thread t = new Thread(task);
    t.start();//w  w  w  .  j a va 2s .  co  m
    try {
        System.out.println(task.get() + " matching files.");
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static <T> FutureTask<T> callLater(final Callable<T> callable) {
    final FutureTask<T> futureTask = new FutureTask<>(callable);
    invokeLater(futureTask);//w  w w .j a  v  a2 s.c om
    return futureTask;
}

From source file:Main.java

public static <T> FutureTask<T> invokeLater(Callable<T> doRun) {
    FutureTask<T> task = new FutureTask<>(doRun);
    invokeLater(task);/*from ww w.j  a v a 2s. c o  m*/
    return task;
}

From source file:Main.java

public static <T> T callInMainThread(Callable<T> call) throws ExecutionException, InterruptedException {
    FutureTask<T> task = new FutureTask<T>(call);
    handler.post(task);//  w w  w  .j  a v  a2  s  .  co  m
    return task.get();
}

From source file:Main.java

public static <T> T callInMainThread(Callable<T> call) throws ExecutionException, InterruptedException {
    FutureTask<T> task = new FutureTask<T>(call);
    sHandler.post(task);// w w w .j a v a 2  s  .  c om
    return task.get();
}

From source file:Main.java

public static <R> R runOnMainSyncAndGetResult(Instrumentation instrumentation, Callable<R> callable)
        throws Throwable {
    FutureTask<R> task = new FutureTask<R>(callable);
    instrumentation.runOnMainSync(task);
    try {/*from w w w .j a  v a 2 s  .co  m*/
        return task.get();
    } catch (ExecutionException e) {
        // Unwrap the cause of the exception and re-throw it.
        throw e.getCause();
    }
}

From source file:Main.java

public static <T> T callAndWait(final Callable<T> callable)
        throws InvocationTargetException, InterruptedException, ExecutionException {
    final FutureTask<T> futureTask = new FutureTask<>(callable);
    invokeAndWait(futureTask);//from  w w  w.  j  a  va  2s  . com
    return futureTask.get();
}

From source file:Main.java

private static <T> T callWithTimeout(final Callable<T> c, long timeout, ExecutorService timeoutExecutor)
        throws InterruptedException, ExecutionException, TimeoutException {
    FutureTask<T> task = new FutureTask<T>(c);
    timeoutExecutor.execute(task);/*from   ww  w .j a va2s  . co m*/
    return task.get(timeout, TimeUnit.MILLISECONDS);
}

From source file:Main.java

public static <T> T runAndWait(Callable<T> callable) throws InterruptedException, ExecutionException {
    FutureTask<T> future = new FutureTask<T>(callable);
    Platform.runLater(future);/*from  ww  w. j  a v  a2s.c  o  m*/
    return future.get();
}

From source file:Main.java

/**
 * Runs the given <tt>Callable</tt> on the JavaFX Application Thread. The method blocks until the <tt>Callable</tt> is executed completely. The return value of the call() method of the callable will be returned
 * You should use the {@link io.datafx.core.concurrent.ProcessChain} for concurrent tasks and background tasks
 * instead of using this low level API.//w  w  w  . j  a va2s  .  c  o m
 *
 * @param callable the callable that will be executed on the JavaFX Application Thread
 * @param <T>      return type of the callable
 * @return return value of the executed call() method of the <tt>Callable</tt>
 * @throws InterruptedException if the JavaFX Application Thread was interrupted while waiting
 * @throws ExecutionException   if the call of the run method of the <tt>Callable</tt> threw an exception
 */
public static <T> T runCallableAndWait(Callable<T> callable) throws InterruptedException, ExecutionException {
    FutureTask<T> future = new FutureTask<T>(callable);
    Platform.runLater(future);
    return future.get();
}