List of usage examples for java.util.concurrent Future get
V get() throws InterruptedException, ExecutionException;
From source file:no.ntnu.idi.socialhitchhiking.map.RouteProvider.java
/** * Returning a {@link MapRoute}, containing data that is retrieved from Google Maps. * //from www .ja v a 2 s . c o m * @param fromLat The latitude where the route starts. * @param fromLon The longitude where the route starts. * @param toLat The latitude where the route ends. * @param toLon The latitude where the route ends. * @return Returns a {@link MapRoute} containing all the map data needed for showing a route in a map view. * @throws MalformedURLException * @throws ParserConfigurationException * @throws SAXException * @throws IOException * @throws XmlPullParserException */ public static MapRoute getRoute(double fromLat, double fromLon, double toLat, double toLon, final boolean drawable) throws MalformedURLException, IOException, XmlPullParserException { final String url = RouteProvider.getUrl(fromLat, fromLon, toLat, toLon); ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<MapRoute> callable = new Callable<MapRoute>() { @Override public MapRoute call() throws ClientProtocolException, IOException, XmlPullParserException { InputStream is = RouteProvider.getConnectionInputStream(url); MapRoute temp = new MapRoute(); temp = RouteProvider.getRoute(is, drawable); return temp; } }; Future<MapRoute> future = executor.submit(callable); MapRoute ret; try { ret = future.get(); } catch (InterruptedException e) { ret = null; } catch (ExecutionException e) { // TODO Auto-generated catch block ret = null; } executor.shutdown(); return ret; }
From source file:com.antsdb.saltedfish.util.UberUtil.java
public static <T> List<T> runParallel(int nThreads, Callable<T> callback) throws Exception { ExecutorService pool = Executors.newFixedThreadPool(nThreads); List<Future<T>> futures = new ArrayList<>(); for (int i = 0; i < nThreads; i++) { Callable<T> run = (Callable<T>) UberUtil.clone(callback); futures.add(pool.submit(run));//from ww w . j a va2 s . c om } List<T> result = new ArrayList<>(); for (Future<T> i : futures) { result.add(i.get()); } pool.shutdown(); return result; }
From source file:fi.luontola.cqrshotel.framework.EventStoreContract.java
private static void repeatInParallel(int iterations, Runnable task, Runnable invariantChecker) throws Exception { final int PARALLELISM = 10; ExecutorService executor = Executors.newFixedThreadPool(PARALLELISM + 1); Future<?> checker;/*from www . ja v a 2s . c o m*/ try { checker = executor.submit(() -> { while (!Thread.interrupted()) { invariantChecker.run(); Thread.yield(); } }); List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < iterations; i++) { futures.add(executor.submit(task)); } for (Future<?> future : futures) { // will throw ExecutionException if there was a problem future.get(); } } finally { executor.shutdownNow(); executor.awaitTermination(10, TimeUnit.SECONDS); } // will throw ExecutionException if there was a problem checker.get(10, TimeUnit.SECONDS); }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static void waitOnFutures(Iterable<Future<?>> futures) { for (Future<?> f : futures) { try {/*from w w w . j ava 2s.com*/ f.get(); } catch (ExecutionException ee) { throw new RuntimeException(ee); } catch (InterruptedException ie) { throw new AssertionError(ie); } } }
From source file:com.l2jfree.util.concurrent.L2ThreadPool.java
public static List<Future<?>> invokeAll(Iterable<Runnable> c) { final List<Future<?>> futures = new ArrayList<Future<?>>(); for (Runnable r : c) futures.add(submit(r));// w w w. j a va 2 s.c o m for (Future<?> future : futures) { try { future.get(); } catch (Exception e) { e.printStackTrace(); } } return futures; }
From source file:com.l2jfree.util.concurrent.L2ThreadPool.java
public static List<Future<?>> invokeAllLongRunning(Iterable<? extends Runnable> c) { final List<Future<?>> futures = new ArrayList<Future<?>>(); for (Runnable r : c) futures.add(submitLongRunning(r)); for (Future<?> future : futures) { try {//w w w .ja v a 2 s . c o m future.get(); } catch (Exception e) { e.printStackTrace(); } } return futures; }
From source file:edu.iu.daal_pca.PCAUtil.java
/** * Generate data and upload to the data dir. * * @param numOfDataPoints/*from w w w . ja v a 2 s .co m*/ * @param vectorSize * @param numPointFiles * @param localInputDir * @param fs * @param dataDir * @throws IOException * @throws InterruptedException * @throws ExecutionException */ static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, 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); } // 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 DataGenRunnable(pointsPerFile, localInputDir, Integer.toString(k), vectorSize)); Future<?> f = service .submit(new DataGenMMDense(pointsPerFile, localInputDir, Integer.toString(k), vectorSize)); 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); DeleteFileFolder(localInputDir); }
From source file:com.netflix.dyno.connectionpool.impl.lb.CircularListTest.java
private static Map<Integer, Integer> getTotalMap(List<Future<Map<Integer, Integer>>> futures) throws InterruptedException, ExecutionException { Map<Integer, Integer> totalMap = new HashMap<Integer, Integer>(); for (Future<Map<Integer, Integer>> f : futures) { Map<Integer, Integer> map = f.get(); for (Integer element : map.keySet()) { Integer count = totalMap.get(element); if (count == null) { totalMap.put(element, map.get(element)); } else { totalMap.put(element, map.get(element) + count); }// w w w .jav a 2 s. com } } return totalMap; }
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Retrieves a {@link List} of addresses that match the given {@link GeoPoint}. * The first element in the list has the best match (but is not guaranteed to be correct). <br><br> * //from ww w . jav a2 s.co m * This method tries to use the {@link Geocoder} to transform a (latitude, longitude) * coordinate into addresses, and if this fails (witch it most likely will under emulation), it * tries to use a method from the {@link GeoHelper}-class. * * @param location The location that is transformed into a list of addresses * @param maxResults The maximum number of addresses to retrieve (should be small). * @param maxAddressLines The maximum number of lines in the addresses. This should be high if you want a complete address! If it is smaller than the total number of lines in the address, it cuts off the last part...) * @return Returns the {@link List} of addresses (as {@link String}s). */ public static List<String> getAddressesAtPoint(final GeoPoint location, final int maxResults, int maxAddressLines) { List<String> addressList = new ArrayList<String>(); List<Address> possibleAddresses = new ArrayList<Address>(); Address address = new Address(Locale.getDefault()); String addressString = "Could not find the address..."; ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<List<Address>> callable = new Callable<List<Address>>() { @Override public List<Address> call() throws IOException { return fancyGeocoder.getFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } }; Future<List<Address>> future = executor.submit(callable); try { possibleAddresses = future.get(); } catch (InterruptedException e1) { possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } catch (ExecutionException e1) { possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } executor.shutdown(); if (possibleAddresses.size() > 0) { for (int i = 0; i < possibleAddresses.size(); i++) { addressString = ""; address = possibleAddresses.get(i); for (int j = 0; j <= address.getMaxAddressLineIndex() && j <= maxAddressLines; j++) { addressString += address.getAddressLine(j); addressString += "\n"; } addressList.add(addressString.trim()); } } return addressList; }
From source file:edu.iu.kmeans.regroupallgather.KMUtil.java
/** * Generate data and upload to the data dir. * /* www. j a va 2 s. c o m*/ * @param numOfDataPoints * @param vectorSize * @param numPointFiles * @param localInputDir * @param fs * @param dataDir * @throws IOException * @throws InterruptedException * @throws ExecutionException */ static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, 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); } // 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 DataGenRunnable(pointsPerFile, localInputDir, Integer.toString(k), vectorSize)); 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); }