Example usage for java.util.concurrent Future get

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

Introduction

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

Prototype

V get() throws InterruptedException, ExecutionException;

Source Link

Document

Waits if necessary for the computation to complete, and then retrieves its result.

Usage

From source file:Main.java

/**
 * Blocks and waits for all Futures in the given collection to complete.
 * //from w w w . j a v  a2 s  . c o  m
 * @param futures the collection of Futures.
 */
public static void waitForCompletion(Collection<Future<?>> futures) {
    for (Future<?> future : futures) {
        try {
            future.get();
        } catch (ExecutionException ex) {
            throw new RuntimeException("Exception during execution", ex);
        } catch (InterruptedException ex) {
            throw new RuntimeException("Thread interrupted", ex);
        }
    }
}

From source file:Main.java

public static long pmax(final long[][] arr, int numThreads) {
    ExecutorService pool = Executors.newFixedThreadPool(numThreads);
    try {/*w  w w .j a  v  a 2  s  .co  m*/
        List<Future<Long>> list = new ArrayList<Future<Long>>();
        for (int i = 0; i < arr.length; i++) {
            final long[] subArr = arr[i];
            list.add(pool.submit(new Callable<Long>() {
                public Long call() {
                    long max = Long.MIN_VALUE;
                    for (int j = 0; j < subArr.length; j++) {
                        if (subArr[j] > max) {
                            max = subArr[j];
                        }
                    }
                    return max;
                }
            }));
        }
        // find the max of each slice's max:
        long max = Long.MIN_VALUE;
        for (Future<Long> future : list) {
            long threadMax = future.get();
            System.out.println("threadMax: " + threadMax);
            if (threadMax > max) {
                max = threadMax;
            }
        }
        return max;
    } catch (Exception e) {
        System.out.println(e);
        return -1;
    } finally {
        pool.shutdown();
    }
}

From source file:Main.java

public static void getAll(List<Future<Object>> futures) {
    for (final Future future : futures) {
        try {/*from   w ww .j  av a 2s.c om*/
            future.get();
        } catch (ExecutionException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:Main.java

static void doUpdate() {
    if (lock.tryLock()) {
        updating = true;// w ww  .  ja  v a  2  s .c o m
        try {
            for (Future<Integer> future : futureList) {
                System.out.println(future.get());
            }
            futureList.clear();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println();
            lock.unlock();
            updating = false;
        }
    }
}

From source file:Main.java

/**
 * Await the completion of tasks interruptibly. The execution
 * exceptions are combined into a single exception, but interrupted
 * exception breaks the wait.//from   w  ww. j a va 2  s  . c o  m
 * @param tasks the tasks to wait
 * @throws ExecutionException on execution errors
 * @throws InterruptedException when the wait is interrupted
 */
public static void awaitAllInterruptibly(Iterable<? extends Future<?>> tasks)
        throws ExecutionException, InterruptedException {
    ExecutionException ee = null;
    for (Future<?> f : tasks) {
        try {
            f.get();
        } catch (ExecutionException ex) {
            if (ee != null) {
                ee.addSuppressed(ex);
            } else {
                ee = ex;
            }
        }
    }
    if (ee != null) {
        throw ee;
    }
}

From source file:Main.java

public static <T> List<T> executeParallel(final List<Callable<T>> callables, final int maxThreadCount)
        throws InterruptedException, ExecutionException {
    final int threadCount = callables.size() > 0 && callables.size() < maxThreadCount ? callables.size()
            : maxThreadCount;/*w  ww  . j av a2  s .  co  m*/
    ExecutorService executor = newFixedThreadPool(threadCount);
    List<T> results = new ArrayList<>();
    try {
        for (Future<T> future : executor.invokeAll(callables)) {
            results.add(future.get());
        }
    } finally {
        executor.shutdown();
    }
    return results;
}

From source file:Test.java

private static void clientStart() {
    try {/*from   w  w w  .  ja  v  a  2 s.c  om*/
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel.open();
        Future<Void> connectFuture = clientSocketChannel.connect(hostAddress);
        connectFuture.get(); // Wait until connection is done.
        OutputStream os = Channels.newOutputStream(clientSocketChannel);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        for (int i = 0; i < 5; i++) {
            oos.writeObject("Look at me " + i);
            Thread.sleep(1000);
        }
        oos.writeObject("EOF");
        oos.close();
        clientSocketChannel.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * <p>inokeInOtherThread</p>
 *
 * @param callable a Callable object./*from   w w w  . j av  a 2  s.  com*/
 * @return a T object.
 *
 * @throws ExecutionException   if any.
 * @throws InterruptedException if any.
 */
@Nullable
public static <T> T inokeInOtherThread(@Nonnull Callable<T> callable)
        throws ExecutionException, InterruptedException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        Future<T> future = executor.submit(callable);
        return future.get();
    } finally {
        executor.shutdown();
    }
}

From source file:Main.java

public static <T> void iteratorResult(List<Future<T>> results) {
    // TODO Auto-generated method stub
    for (Future<T> future : results) {
        try {// ww w. j a  va  2s  .  c  o m
            System.out.println(future.isDone() + "," + future.get());
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.dsclab.loader.app.Loader.java

private static void waitAllDone(List<Future<String>> ths) throws InterruptedException, ExecutionException {
    for (Future<String> f : ths) {
        f.get();
    }/*from  ww w.ja v a 2  s. c  o  m*/
}