List of usage examples for java.util.concurrent Future get
V get() throws InterruptedException, ExecutionException;
From source file:Main.java
static <T> List<T> submit(List<? extends Callable<T>> taskList, final String taskName) throws Exception { List<Future<T>> futureList = new ArrayList(); List<T> result = new ArrayList<>(); ExecutorService pool = newFixedThreadPool(taskList.size(), taskName); for (Callable<T> callable : taskList) { futureList.add(pool.submit(callable)); }/*w w w .ja va2 s . c o m*/ pool.shutdown(); for (Future<T> f : futureList) { try { result.add(f.get()); } catch (Exception e) { e.printStackTrace(); } } return result; }
From source file:Main.java
/** * Await all of the future tasks indefinitely. * The exceptions are combined into a single exception * (by adding subsequent exceptions as addsuppressed). * @param tasks the sequence of tasks//from w w w . j ava2 s .c om * @throws ExecutionException if the task failed * @throws InterruptedException if the wait was interrupted */ public static void awaitAll(Iterable<? extends Future<?>> tasks) throws ExecutionException, InterruptedException { ExecutionException ee = null; InterruptedException ie = null; for (Future<?> f : tasks) { try { f.get(); } catch (ExecutionException ex) { if (ee != null) { ee.addSuppressed(ex); } else { ee = ex; } } catch (InterruptedException ex) { if (ie != null) { ie.addSuppressed(ex); } else { ie = ex; } } } if (ee != null && ie != null) { ee.addSuppressed(ie); throw ee; } else if (ee != null) { throw ee; } else if (ie != null) { throw ie; } }
From source file:Main.java
/** * Verify that no assertions have failed inside a future. * Used for unit tests that spawn threads. E.g., * <p>/* w ww .j av a2s .c om*/ * <code> * List<Future<Void>> results = Lists.newArrayList(); * Future<Void> f = executor.submit(new Callable<Void> { * public Void call() { * assertTrue(someMethod()); * } * }); * results.add(f); * assertOnFutures(results); * </code> * @param threadResults A list of futures * @param <T> * @throws InterruptedException If interrupted when waiting for a result * from one of the futures * @throws ExecutionException If an exception other than AssertionError * occurs inside any of the futures */ public static <T> void assertOnFutures(List<Future<T>> threadResults) throws InterruptedException, ExecutionException { for (Future<T> threadResult : threadResults) { try { threadResult.get(); } catch (ExecutionException e) { if (e.getCause() instanceof AssertionError) { throw (AssertionError) e.getCause(); } throw e; } } }
From source file:Main.java
public static void invokeAll(final List<Runnable> tasks, final ExecutorService executor) throws InterruptedException, ExecutionException { ExecutionException saved = null; if (executor != null) { final List<Future<?>> futures = new ArrayList<>(); for (final Runnable task : tasks) futures.add(executor.submit(task)); for (final Future<?> future : futures) { try { future.get(); } catch (InterruptedException e) { throw e; } catch (ExecutionException e) { if (saved == null) { saved = e;/*from ww w . j a v a2s. c om*/ } else { saved.addSuppressed(e); } } } } else { for (final Runnable task : tasks) { try { task.run(); } catch (Exception e) { if (saved == null) { saved = new ExecutionException(e); } else { saved.addSuppressed(e); } } } } if (saved != null) throw saved; }
From source file:Test.java
private static void serverStart() { try {/*from ww w . j a v a 2 s . c om*/ InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583); AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open() .bind(hostAddress); Future<AsynchronousSocketChannel> serverFuture = serverSocketChannel.accept(); final AsynchronousSocketChannel clientSocket = serverFuture.get(); if ((clientSocket != null) && (clientSocket.isOpen())) { InputStream connectionInputStream = Channels.newInputStream(clientSocket); ObjectInputStream ois = null; ois = new ObjectInputStream(connectionInputStream); while (true) { Object object = ois.readObject(); if (object.equals("EOF")) { clientSocket.close(); break; } System.out.println("Received :" + object); } ois.close(); connectionInputStream.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.edgexfoundry.EdgeXConfigWatcherApplication.java
public static void registerWatcher(List<CatalogService> services, String notificationPath) { try (CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();) { httpClient.start();//from w w w.java 2 s. c o m for (CatalogService service : services) { LOGGER.info("Attempting registration of {} to: {}", service.getServiceName(), notificationPath); URL serviceNotificationURL = new URL(notificationProtocol, service.getAddress(), service.getServicePort(), notificationPath); HttpGet request = new HttpGet(serviceNotificationURL.toURI()); Future<HttpResponse> future = httpClient.execute(request, null); future.get(); LOGGER.info("Registered {} @ {}", service.getServiceName(), notificationPath); } } catch (URISyntaxException e) { LOGGER.error("URI Syntax issue: " + e.getMessage()); } catch (InterruptedException e) { LOGGER.error("Interrupted process: " + e.getMessage()); Thread.currentThread().interrupt(); } catch (ExecutionException e) { LOGGER.error("Execution problem: " + e.getMessage()); } catch (IOException e) { LOGGER.error("IO problem: " + e.getMessage()); } }
From source file:Main.java
public static <V> V getUninterruptibly(Future<V> future) throws ExecutionException { boolean interrupted = false; try {/* www . j ava 2 s .c o m*/ while (true) { try { return future.get(); } catch (InterruptedException e) { interrupted = true; } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
From source file:cn.clxy.codes.upload.UploadFileService.java
private static String checkFuture(Future<String> future) { try {/*from w w w .j av a 2 s. c om*/ String result = future.get(); log.debug(result + " is done."); return result; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return null; } catch (ExecutionException e) { throw new RuntimeException(e.getCause()); } }
From source file:Main.java
public static <T> T getFutureResult(Callable<T> task, long timeout) throws TimeoutException { try {// ww w.j a v a 2 s .com ExecutorService executor = Executors.newFixedThreadPool(1); Future<T> future = executor.submit(task); T result = null; if (timeout <= 0) { System.out.println("get wait forever"); result = future.get(); } else { System.out.println("get wait " + timeout); result = future.get(timeout, TimeUnit.MILLISECONDS); } return result; } catch (java.util.concurrent.TimeoutException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.bigdata.dastor.streaming.StreamOut.java
/** * Split out files for all tables on disk locally for each range and then stream them to the target endpoint. *///from w w w.ja v a2 s .c o m public static void transferRanges(InetAddress target, String tableName, Collection<Range> ranges, Runnable callback) { assert ranges.size() > 0; logger.debug("Beginning transfer process to " + target + " for ranges " + StringUtils.join(ranges, ", ")); /* * (1) dump all the memtables to disk. * (2) anticompaction -- split out the keys in the range specified * (3) transfer the data. */ try { Table table = Table.open(tableName); logger.info("Flushing memtables for " + tableName + "..."); for (Future f : table.flush()) { try { f.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } } logger.info("Performing anticompaction ..."); /* Get the list of files that need to be streamed */ transferSSTables(target, table.forceAntiCompaction(ranges, target), tableName); // SSTR GC deletes the file when done } catch (IOException e) { throw new IOError(e); } finally { StreamOutManager.remove(target); } if (callback != null) callback.run(); }