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: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();/*from w  ww  .j  a  v a 2 s . co  m*/
    try {
        System.out.println(task.get() + " matching files.");
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    }
}

From source file:hydrograph.server.execution.tracking.client.main.HydrographMain.java

/**
 * The main method.//from   w  ww  .  ja va 2  s. co m
 *
 * @param args
 *            the arguments
 * @throws Exception
 *             the exception
 */
public static void main(String[] args) throws Exception {

    HydrographMain hydrographMain = new HydrographMain();
    final Timer timer = new Timer();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        Session session = null;
        boolean isExecutionTracking = false;
        String[] argsList = args;
        List<String> argumentList = new ArrayList<String>(Arrays.asList(args));
        final String jobId = hydrographMain.getJobId(argumentList);

        getLogLevel(argumentList).ifPresent(x -> {
            if (!x.equalsIgnoreCase(String.valueOf(logger.getLevel()))) {
                setLoglevel(x);
            } else {
                Optional.empty();
            }

        });

        logger.info("Argument List: " + argumentList.toString());

        String trackingClientSocketPort = hydrographMain.getTrackingClientSocketPort(argumentList);

        if (argumentList.contains(Constants.IS_TRACKING_ENABLE)) {
            int index = argumentList.indexOf(Constants.IS_TRACKING_ENABLE);
            isExecutionTracking = Boolean.valueOf(argsList[index + 1]);
            argumentList = removeItemFromIndex(index, argumentList);
        }

        if (argumentList.contains(Constants.TRACKING_CLIENT_SOCKET_PORT)) {
            int index = argumentList.indexOf(Constants.TRACKING_CLIENT_SOCKET_PORT);
            argumentList = removeItemFromIndex(index, argumentList);
        }

        argsList = argumentList.toArray(new String[argumentList.size()]);

        logger.debug("Execution tracking enabled - " + isExecutionTracking);
        logger.info("Tracking Client Port: " + trackingClientSocketPort);

        /**
         * Start new thread to run job
         */
        final HydrographService execution = new HydrographService();

        FutureTask task = hydrographMain.executeGraph(latch, jobId, argsList, execution, isExecutionTracking);

        hydrographMain.executorService = Executors.newSingleThreadExecutor();
        hydrographMain.executorService.submit(task);

        if (isExecutionTracking) {
            //If tracking is enabled, start to post execution tracking status.
            final HydrographEngineCommunicatorSocket socket = new HydrographEngineCommunicatorSocket(execution);
            session = hydrographMain.connectToServer(socket, jobId, trackingClientSocketPort);
            hydrographMain.sendExecutionTrackingStatus(latch, session, jobId, timer, execution, socket);
        }

        //waiting for execute graph thread 
        task.get();

    } catch (Exception exp) {
        logger.info("Getting exception from HydrographMain");
        throw new RuntimeException(exp);
    } finally {
        //cleanup threads --> executor thread and timer thread 
        logger.info("HydrographMain releasing resources");
        if (!hydrographMain.executorService.isShutdown() && !hydrographMain.executorService.isTerminated()) {
            hydrographMain.executorService.shutdown();
        }
        timer.cancel();

    }
}

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 ww  . j a v a  2  s.  c  o  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.jav a 2  s .com*/
    return task.get();
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void runAndWait(Runnable runnable) throws InterruptedException, ExecutionException {
    FutureTask<?> future = new FutureTask(runnable, null);
    Platform.runLater(future);/*from  w  w w .j  av  a 2 s . c  om*/
    future.get();
}

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 w  ww  . j a  v a2 s .  c  om*/
    return future.get();
}

From source file:Main.java

/**
 * Runs the given <tt>Runnable</tt> on the JavaFX Application Thread. The method blocks until the <tt>Runnable</tt> is executed completely.
 * You should use the {@link io.datafx.core.concurrent.ProcessChain} for concurrent tasks and background tasks
 * instead of using this low level API.//from w w  w.  j  av a2  s.c  o  m
 *
 *
 * @param runnable the runnable that will be executed on the JavaFX Application Thread
 * @throws InterruptedException if the JavaFX Application Thread was interrupted while waiting
 * @throws ExecutionException   if the call of the run method of the <tt>Runnable</tt> threw an exception
 */
public static void runAndWait(Runnable runnable) throws InterruptedException, ExecutionException {
    FutureTask<Void> future = new FutureTask<>(runnable, null);
    Platform.runLater(future);
    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.//from  ww w .j  a  va2s  .co 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();
}

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 ww. ja v a  2  s .  com*/
    return futureTask.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 {//www . ja  va  2s  . c o m
        return task.get();
    } catch (ExecutionException e) {
        // Unwrap the cause of the exception and re-throw it.
        throw e.getCause();
    }
}