List of usage examples for java.util.concurrent Future get
V get() throws InterruptedException, ExecutionException;
From source file:com.bigdata.dastor.utils.FBUtilities.java
public static void waitOnFutures(Collection<Future<?>> futures) { for (Future f : futures) { try {/*from ww w.j a v a2 s . c o m*/ f.get(); } catch (ExecutionException ee) { throw new RuntimeException(ee); } catch (InterruptedException ie) { throw new AssertionError(ie); } } }
From source file:edu.iu.daal_naive.NaiveUtil.java
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, int nClasses, String localInputDir, FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException { int pointsPerFile = numOfDataPoints / numPointFiles; System.out.println("Writing " + pointsPerFile + " vectors to a file"); // Check data directory if (fs.exists(dataDir)) { fs.delete(dataDir, true);/*from w ww . j a va 2 s. c o m*/ } // Check local directory File localDir = new File(localInputDir); // If existed, regenerate data if (localDir.exists() && localDir.isDirectory()) { for (File file : localDir.listFiles()) { file.delete(); } localDir.delete(); } boolean success = localDir.mkdir(); if (success) { System.out.println("Directory: " + localInputDir + " created"); } if (pointsPerFile == 0) { throw new IOException("No point to write."); } // Create random data points int poolSize = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(poolSize); List<Future<?>> futures = new LinkedList<Future<?>>(); for (int k = 0; k < numPointFiles; k++) { Future<?> f = service.submit( new DataGenNaiveBayes(localInputDir, Integer.toString(k), pointsPerFile, vectorSize, nClasses)); futures.add(f); // add a new thread } for (Future<?> f : futures) { f.get(); } // Shut down the executor service so that this // thread can exit service.shutdownNow(); // Wrap to path object Path localInput = new Path(localInputDir); fs.copyFromLocalFile(localInput, dataDir); }
From source file:edu.cornell.med.icb.R.RUtils.java
/** * @param mode Mode to execute/* w ww. j av a 2 s . c om*/ * @param threadPool The ExecutorService used to start the Rserve process * @param command Full path to command used to start Rserve process * @param host Host where the command should be sent * @param port Port number where the command should be sent * @param username Username to send to the server if authentication is required * @param password Password to send to the server if authentication is required * @return 0 if the mode was executed successfully */ private static int executeMode(final Mode mode, final ExecutorService threadPool, final String host, final int port, final String username, final String password, final String command) { int status = 0; switch (mode) { case shutdown: try { shutdown(host, port, username, password); } catch (RserveException e) { // just let the user know and try the other servers LOG.warn("Couldn't shutdown Rserve on " + host + ":" + port, e); status = 1; } break; case startup: final Future<Integer> future = startup(threadPool, command, host, port, username, password); try { status = future.get(); } catch (ExecutionException e) { LOG.warn("Couldn't shutdown Rserve on " + host + ":" + port, e); status = 2; } catch (InterruptedException e) { LOG.error("Interrupted!", e); Thread.currentThread().interrupt(); } break; case validate: final boolean connectionIsOk = validate(host, port, username, password); System.out.println("Rserve on " + host + ":" + port + " is " + (connectionIsOk ? "UP" : "DOWN")); status = connectionIsOk ? 0 : 42; break; default: throw new IllegalArgumentException("Unknown mode: " + mode); } return status; }
From source file:com.offbynull.portmapper.upnpigd.UpnpIgdDiscovery.java
private static Map<UpnpIgdDevice, byte[]> getRootXmlForEachDevice(Set<UpnpIgdDevice> devices) throws InterruptedException { Map<UpnpIgdDevice, byte[]> serviceRoots = new HashMap(); ExecutorService executorService = null; try {/*from ww w . j av a 2 s . co m*/ int maximumPoolSize = (int) ((double) Runtime.getRuntime().availableProcessors() / (1.0 - 0.95)); executorService = new ThreadPoolExecutor(0, maximumPoolSize, 1, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); List<HttpRequestCallable<UpnpIgdDevice>> tasks = new LinkedList<>(); for (UpnpIgdDevice device : devices) { tasks.add(new HttpRequestCallable<>(device.getUrl(), device)); } List<Future<Pair<UpnpIgdDevice, byte[]>>> results = executorService.invokeAll(tasks); for (Future<Pair<UpnpIgdDevice, byte[]>> result : results) { try { Pair<UpnpIgdDevice, byte[]> data = result.get(); serviceRoots.put(data.getKey(), data.getValue()); } catch (InterruptedException | ExecutionException | CancellationException e) { // NOPMD // do nothing, skip } } } finally { if (executorService != null) { executorService.shutdownNow(); } } return serviceRoots; }
From source file:Main.java
public static String getDomainAddress(final String domain) { try {/*from w w w .ja v a 2 s. com*/ ExecutorService exec = Executors.newCachedThreadPool(); Future<String> fs = exec.submit(new Callable<String>() { @Override public String call() throws Exception { InetAddress inetAddress; try { inetAddress = InetAddress.getByName(domain); return inetAddress.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } }); return fs.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return null; }
From source file:de.tu_dortmund.ub.data.dswarm.TaskProcessingUnit.java
private static void execute(final String serviceName, final LinkedList<Callable<String>> threads, final ThreadPoolExecutor pool) throws InterruptedException, java.util.concurrent.ExecutionException { try {//from w w w . j a va 2 s .co m final List<Future<String>> futureList = pool.invokeAll(threads); for (final Future<String> f : futureList) { final String message = f.get(); final String message1 = String.format("[%s] %s", serviceName, message); LOG.info(message1); } } catch (final Exception e) { LOG.error("something went wrong", e); throw e; } finally { pool.shutdown(); } }
From source file:com.offbynull.portmapper.upnpigd.UpnpIgdDiscovery.java
private static Map<UpnpIgdServiceReference, byte[]> getServiceDescriptions( Set<UpnpIgdServiceReference> services) throws InterruptedException { Map<UpnpIgdServiceReference, byte[]> serviceXmls = new HashMap(); ExecutorService executorService = null; try {// w ww.j a v a 2s .c o m int maximumPoolSize = (int) ((double) Runtime.getRuntime().availableProcessors() / (1.0 - 0.95)); executorService = new ThreadPoolExecutor(0, maximumPoolSize, 1, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); List<HttpRequestCallable<UpnpIgdServiceReference>> tasks = new LinkedList<>(); for (UpnpIgdServiceReference service : services) { tasks.add(new HttpRequestCallable<>(service.getScpdUrl(), service)); } List<Future<Pair<UpnpIgdServiceReference, byte[]>>> results = executorService.invokeAll(tasks); for (Future<Pair<UpnpIgdServiceReference, byte[]>> result : results) { try { Pair<UpnpIgdServiceReference, byte[]> data = result.get(); serviceXmls.put(data.getKey(), data.getValue()); } catch (InterruptedException | ExecutionException | CancellationException e) { // NOPMD // do nothing, skip } } } finally { if (executorService != null) { executorService.shutdownNow(); } } return serviceXmls; }
From source file:com.twitter.graphjet.bipartite.GraphConcurrentTestHelper.java
/** * This helper method sets up a concurrent read-write situation with a single writer and multiple * readers that access the same underlying bipartiteGraph, and tests for correct edge access during * simultaneous edge writes. This helps test read consistency during arbitrary points of * inserting edges. Note that the exact read-write sequence here is non-deterministic and would * vary depending on the machine, but the hope is that given the large number of readers the reads * would be done at many different points of edge insertion. The test itself checks only for * partial correctness (it could have false positives) so this should only be used as a supplement * to other testing./* w w w . j a v a 2 s . c om*/ * * @param graph is the underlying * {@link BipartiteGraph} * @param numReadersPerNode is the number of reader threads to use per node * @param leftSize is the number of left nodes * @param rightSize is the number of right nodes * @param edgeProbability is the probability of an edge between a left-right node pair * @param random is the random number generator to use for generating a random graph */ public static <T extends BipartiteGraph & DynamicBipartiteGraph> void testRandomConcurrentReadWriteThreads( T graph, int numReadersPerNode, int leftSize, int rightSize, double edgeProbability, Random random) { int maxWaitingTimeForThreads = 20; // in milliseconds int numLeftReaders = leftSize * numReadersPerNode; int numRightReaders = rightSize * numReadersPerNode; int totalNumReaders = numLeftReaders + numRightReaders; CountDownLatch readersDoneLatch = new CountDownLatch(totalNumReaders); // First, construct a random set of edges to insert in the graph Set<Pair<Long, Long>> edges = Sets .newHashSetWithExpectedSize((int) (leftSize * rightSize * edgeProbability)); List<BipartiteGraphReader> leftReaders = Lists.newArrayListWithCapacity(numLeftReaders); List<BipartiteGraphReader> rightReaders = Lists.newArrayListWithCapacity(numRightReaders); Long2ObjectMap<LongSet> leftSideGraph = new Long2ObjectOpenHashMap<LongSet>(leftSize); Long2ObjectMap<LongSet> rightSideGraph = new Long2ObjectOpenHashMap<LongSet>(leftSize); int averageLeftDegree = (int) (rightSize * edgeProbability); for (int i = 0; i < leftSize; i++) { LongSet nodeEdges = new LongOpenHashSet(averageLeftDegree); for (int j = 0; j < rightSize; j++) { if (random.nextDouble() < edgeProbability) { nodeEdges.add(j); if (!rightSideGraph.containsKey(j)) { rightSideGraph.put(j, new LongOpenHashSet(new long[] { i })); } else { rightSideGraph.get(j).add(i); } edges.add(Pair.of((long) i, (long) j)); } } leftSideGraph.put(i, nodeEdges); } // Create a bunch of leftReaders per node that'll read from the graph at random for (int i = 0; i < leftSize; i++) { for (int j = 0; j < numReadersPerNode; j++) { leftReaders.add(new BipartiteGraphReader(graph, new CountDownLatch(0), readersDoneLatch, i, true, random.nextInt(maxWaitingTimeForThreads))); } } // Create a bunch of rightReaders per node that'll read from the graph at random for (int i = 0; i < rightSize; i++) { for (int j = 0; j < numReadersPerNode; j++) { rightReaders.add(new BipartiteGraphReader(graph, new CountDownLatch(0), readersDoneLatch, i, false, random.nextInt(maxWaitingTimeForThreads))); } } // Create a single writer that will insert these edges in random order List<WriterInfo> writerInfo = Lists.newArrayListWithCapacity(edges.size()); List<Pair<Long, Long>> edgesList = Lists.newArrayList(edges); Collections.shuffle(edgesList); CountDownLatch writerDoneLatch = new CountDownLatch(edgesList.size()); for (Pair<Long, Long> edge : edgesList) { writerInfo.add(new WriterInfo(edge.getLeft(), edge.getRight(), new CountDownLatch(0), writerDoneLatch)); } ExecutorService executor = Executors.newFixedThreadPool(totalNumReaders + 1); // single writer List<Callable<Integer>> allThreads = Lists.newArrayListWithCapacity(totalNumReaders + 1); // First, we add the writer allThreads.add(Executors.callable(new BipartiteGraphWriter(graph, writerInfo), 1)); // then the readers for (int i = 0; i < numLeftReaders; i++) { allThreads.add(Executors.callable(leftReaders.get(i), 1)); } for (int i = 0; i < numRightReaders; i++) { allThreads.add(Executors.callable(rightReaders.get(i), 1)); } // these will execute in some non-deterministic order Collections.shuffle(allThreads, random); // Wait for all the processes to finish try { List<Future<Integer>> results = executor.invokeAll(allThreads, 10, TimeUnit.SECONDS); for (Future<Integer> result : results) { assertTrue(result.isDone()); assertEquals(1, result.get().intValue()); } } catch (InterruptedException e) { throw new RuntimeException("Execution for a thread was interrupted: ", e); } catch (ExecutionException e) { throw new RuntimeException("Execution issue in an executor thread: ", e); } // confirm that these worked as expected try { readersDoneLatch.await(); writerDoneLatch.await(); } catch (InterruptedException e) { throw new RuntimeException("Execution for a latch was interrupted: ", e); } // Check that all readers' read info is consistent with the graph // first check the left side for (int i = 0; i < numLeftReaders; i++) { LongSet expectedLeftEdges = leftSideGraph.get(leftReaders.get(i).queryNode); assertTrue(leftReaders.get(i).getQueryNodeDegree() <= expectedLeftEdges.size()); if (leftReaders.get(i).getQueryNodeDegree() == 0) { assertNull(leftReaders.get(i).getQueryNodeEdges()); } else { for (long edge : leftReaders.get(i).getQueryNodeEdges()) { assertTrue(expectedLeftEdges.contains(edge)); } } } // then the right side for (int i = 0; i < numRightReaders; i++) { LongSet expectedRightEdges = rightSideGraph.get(rightReaders.get(i).queryNode); assertTrue(rightReaders.get(i).getQueryNodeDegree() <= expectedRightEdges.size()); if (rightReaders.get(i).getQueryNodeDegree() == 0) { assertNull(rightReaders.get(i).getQueryNodeEdges()); } else { for (long edge : rightReaders.get(i).getQueryNodeEdges()) { assertTrue(expectedRightEdges.contains(edge)); } } } }
From source file:disko.DU.java
public static <DocumentType> void analyzeDocument(AnalysisContext<DocumentType> ctx, DocumentType doc, HGHandle scope, DataFlowNetwork<AnalysisContext<DocumentType>> network) { ctx.pushScoping(scope);/* w w w . j a va2s . c om*/ network.setContext(ctx); Future<?> future; future = network.start(); try { future.get(); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:de.tu_dortmund.ub.data.dswarm.TaskProcessingUnit.java
private static String executeTPUTask(final String[] watchFolderFiles, final String resourceWatchFolder, final Optional<String> optionalOutputDataModelID, final Optional<String> optionalExportMimeType, final Optional<String> optionalExportFileExtension, final Integer engineThreads, final String serviceName, final Properties config) throws Exception { // create job list final LinkedList<Callable<String>> transforms = new LinkedList<>(); int cnt = 1;/*w w w.j a v a 2 s . c o m*/ for (final String watchFolderFile : watchFolderFiles) { LOG.info("[{}][{}] do TPU task execution '{}' for file '{}'", serviceName, cnt, cnt, watchFolderFile); transforms.add(new TPUTask(config, watchFolderFile, resourceWatchFolder, optionalOutputDataModelID, optionalExportMimeType, optionalExportFileExtension, serviceName, cnt)); cnt++; } // work on jobs final ThreadPoolExecutor pool = new ThreadPoolExecutor(engineThreads, engineThreads, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); try { final List<Future<String>> futureList = pool.invokeAll(transforms); final StringBuilder resultSB = new StringBuilder(); for (final Future<String> f : futureList) { final String message = f.get(); LOG.info(message); resultSB.append(message).append("\n"); } return resultSB.toString(); } catch (final Exception e) { LOG.error("something went wrong", e); throw e; } finally { pool.shutdown(); } }