List of usage examples for java.util.concurrent Future get
V get() throws InterruptedException, ExecutionException;
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Gets a {@link JSONArray} from the Google Maps API from a set of coordinates * @param lat/*from ww w. j a v a 2s .com*/ * @param lon * @param maxResults * @return JSONArray of locations */ private static JSONArray getJSONArrayFromLocation(final double lat, final double lon, int maxResults) { ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<JSONArray> callable = new Callable<JSONArray>() { @Override public JSONArray call() throws IOException { String urlStr = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lon + "&sensor=false"; String response = ""; HttpClient client = new DefaultHttpClient(); HttpResponse hr = client.execute(new HttpGet(urlStr)); HttpEntity entity = hr.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); String buff = null; while ((buff = br.readLine()) != null) response += buff; br.close(); JSONArray responseArray = null; try { JSONObject jsonObject = new JSONObject(response); responseArray = jsonObject.getJSONArray("results"); } catch (JSONException e) { return null; } return responseArray; } }; Future<JSONArray> future = executor.submit(callable); JSONArray ret; try { ret = future.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block ret = null; } catch (ExecutionException e) { // TODO Auto-generated catch block ret = null; } return ret; }
From source file:edu.iu.daal_svd.SVDUtil.java
/** * Generate data and upload to the data dir. * //from www.ja v a 2s . 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)); 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.twitter.graphjet.bipartite.edgepool.EdgePoolConcurrentTestHelper.java
/** * This helper method sets up a concurrent read-write situation with a single writer and multiple * readers that access the same underlying edgePool, 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.//from w w w . j av a 2 s. c om * * @param edgePool is the underlying * {@link com.twitter.graphjet.bipartite.edgepool.EdgePool} * @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 void testRandomConcurrentReadWriteThreads(EdgePool edgePool, int numReadersPerNode, int leftSize, int rightSize, double edgeProbability, Random random) { int maxWaitingTimeForThreads = 20; // in milliseconds int numReaders = leftSize * numReadersPerNode; CountDownLatch readersDoneLatch = new CountDownLatch(numReaders); // First, construct a random set of edges to insert in the graph Set<Pair<Integer, Integer>> edges = Sets .newHashSetWithExpectedSize((int) (leftSize * rightSize * edgeProbability)); List<EdgePoolReader> readers = Lists.newArrayListWithCapacity(numReaders); Int2ObjectMap<IntSet> leftSideGraph = new Int2ObjectOpenHashMap<IntSet>(leftSize); int averageLeftDegree = (int) (rightSize * edgeProbability); for (int i = 0; i < leftSize; i++) { IntSet nodeEdges = new IntOpenHashSet(averageLeftDegree); for (int j = 0; j < rightSize; j++) { if (random.nextDouble() < edgeProbability) { nodeEdges.add(j); edges.add(Pair.of(i, 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++) { readers.add(new EdgePoolReader(edgePool, new CountDownLatch(0), readersDoneLatch, i, random.nextInt(maxWaitingTimeForThreads))); } } // Create a single writer that will insert these edges in random order List<WriterInfo> writerInfo = Lists.newArrayListWithCapacity(edges.size()); List<Pair<Integer, Integer>> edgesList = Lists.newArrayList(edges); Collections.shuffle(edgesList); CountDownLatch writerDoneLatch = new CountDownLatch(edgesList.size()); for (Pair<Integer, Integer> edge : edgesList) { writerInfo.add(new WriterInfo(edge.getLeft(), edge.getRight(), new CountDownLatch(0), writerDoneLatch)); } ExecutorService executor = Executors.newFixedThreadPool(numReaders + 1); // single writer List<Callable<Integer>> allThreads = Lists.newArrayListWithCapacity(numReaders + 1); // First, we add the writer allThreads.add(Executors.callable(new EdgePoolWriter(edgePool, writerInfo), 1)); // then the readers for (int i = 0; i < numReaders; i++) { allThreads.add(Executors.callable(readers.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 last reader was interrupted: ", e); } // Check that all readers' read info is consistent with the graph for (EdgePoolReader reader : readers) { IntSet expectedEdges = leftSideGraph.get(reader.queryNode); assertTrue(reader.getQueryNodeDegree() <= expectedEdges.size()); if (reader.getQueryNodeDegree() == 0) { assertNull(reader.getQueryNodeEdges()); } else { for (int edge : reader.getQueryNodeEdges()) { assertTrue(expectedEdges.contains(edge)); } } } }
From source file:com.asprise.imaging.core.Imaging.java
/** * Executes and wait indefinitely until the result is returned or exception occurs * @param callable/*from w w w . j a va 2 s . c o m*/ * @param <R> * @return * @throws Throwable in case of exeception occurred during execution */ public static <R> R executeInDefaultExecutorServiceAndWaitTillReturn(Callable<R> callable) throws Throwable { List<Callable<R>> list = new ArrayList<Callable<R>>(); list.add(callable); try { List<Future<R>> futures = getDefaultExecutorServiceForScanning().invokeAll(list); Future<R> returned = futures.get(0); return returned.get(); } catch (Throwable e) { if (e instanceof ExecutionException) { throw ((ExecutionException) e).getCause(); } else { throw e; } } }
From source file:com.echopf.members.ECHOMemberQuery.java
/** * Does Logout//from w ww .j a v a2s . c o m * @param sync if set TRUE, then the main (UI) thread is waited for complete the logging-out in a background thread. * (a synchronous communication) * @param callback invoked after the logging-out is completed * @param instanceId the reference ID of the instance to which the logged-out member belong * @throws ECHOException */ protected static void doLogout(final boolean sync, final ResultCallback callback, final String instanceId) throws ECHOException { final Handler handler = new Handler(); // Get ready a background thread ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<Void> communicator = new Callable<Void>() { @Override public Void call() throws ECHOException { ECHOException exception = null; try { ECHOQuery.postRequest(instanceId + "/login", new JSONObject()); } catch (ECHOException e) { exception = e; } catch (Exception e) { exception = new ECHOException(e); } if (sync == false) { // Execute a callback method in the main (UI) thread. if (callback != null) { final ECHOException fException = exception; handler.post(new Runnable() { @Override public void run() { callback.done(fException); } }); } return null; } else { if (exception == null) return null; throw exception; } } }; Future<Void> future = executor.submit(communicator); if (sync) { try { future.get(); return; } catch (InterruptedException e) { Thread.currentThread().interrupt(); // ignore/reset } catch (ExecutionException e) { Throwable e2 = e.getCause(); if (e2 instanceof ECHOException) { throw (ECHOException) e2; } throw new RuntimeException(e2); } } return; }
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Gets a {@link JSONObject} from an address string * @param adr// w ww. j a v a2s. c o m * @return */ private static JSONObject getLocationInfo(final String adr) { ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<StringBuilder> callable = new Callable<StringBuilder>() { @Override public StringBuilder call() throws ClientProtocolException, IOException { StringBuilder stringBuilder = new StringBuilder(); String address; address = adr.replaceAll(" ", "%20"); address = address.replaceAll("\n", "%20"); HttpPost httppost = new HttpPost( "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); HttpClient client = new DefaultHttpClient(); HttpResponse response; stringBuilder = new StringBuilder(); response = client.execute(httppost); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); int b; while ((b = stream.read()) != -1) { stringBuilder.append((char) b); } return stringBuilder; } }; Future<StringBuilder> future = executor.submit(callable); StringBuilder stringBuilder; try { stringBuilder = future.get(); } catch (InterruptedException e1) { // TODO Auto-generated catch block stringBuilder = new StringBuilder(); } catch (ExecutionException e1) { stringBuilder = new StringBuilder(); } executor.shutdown(); JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } return jsonObject; }
From source file:com.google.code.linkedinapi.client.examples.AsyncApiExample.java
/** * Process command line options and call the service. *///ww w . j av a 2s . c om private static void processCommandLine(CommandLine line, Options options) { if (line.hasOption(HELP_OPTION)) { printHelp(options); } else if (line.hasOption(CONSUMER_KEY_OPTION) && line.hasOption(CONSUMER_SECRET_OPTION) && line.hasOption(ACCESS_TOKEN_OPTION) && line.hasOption(ACCESS_TOKEN_SECRET_OPTION)) { final String consumerKeyValue = line.getOptionValue(CONSUMER_KEY_OPTION); final String consumerSecretValue = line.getOptionValue(CONSUMER_SECRET_OPTION); final String accessTokenValue = line.getOptionValue(ACCESS_TOKEN_OPTION); final String tokenSecretValue = line.getOptionValue(ACCESS_TOKEN_SECRET_OPTION); final LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(consumerKeyValue, consumerSecretValue); final AsyncLinkedInApiClient client = factory.createAsyncLinkedInApiClient(accessTokenValue, tokenSecretValue); try { if (line.hasOption(ID_OPTION)) { String idValue = line.getOptionValue(ID_OPTION); System.out.println("Fetching profile and connections for user with id:" + idValue); Future<Person> profile = client.getProfileById(idValue); Future<Connections> connections = client.getConnectionsById(idValue); System.out.println("Done calling asynchronously. Now waiting for the result."); printResult(profile.get()); printResult(connections.get()); } else if (line.hasOption(EMAIL_OPTION)) { String emailValue = line.getOptionValue(EMAIL_OPTION); System.out.println("Fetching connections for user with email:" + emailValue); // Future<Connections> connections = client.getConnectionsByEmail(emailValue); // printResult(connections.get()); } else if (line.hasOption(URL_OPTION)) { String urlValue = line.getOptionValue(URL_OPTION); System.out.println("Fetching profile and connections for user with url:" + urlValue); Future<Person> profile = client.getProfileByUrl(urlValue, ProfileType.STANDARD); Future<Connections> connections = client.getConnectionsByUrl(urlValue); System.out.println("Done calling asynchronously. Now waiting for the result."); printResult(profile.get()); printResult(connections.get()); } else { System.out.println("Fetching profile and connections for current user."); Future<Person> profile = client.getProfileForCurrentUser(); Future<Connections> connections = client.getConnectionsForCurrentUser(); System.out.println("Done calling asynchronously. Now waiting for the result."); printResult(profile.get()); printResult(connections.get()); } } catch (Exception e) { e.printStackTrace(); } } else { printHelp(options); } }
From source file:de.tu_dortmund.ub.data.util.TPUUtil.java
public static String executeInit(final String initResourceFile, final String serviceName, final Integer engineThreads, final Properties config, final int cnt) throws Exception { // create job final Callable<String> initTask = new Init(initResourceFile, config, cnt); // work on jobs final ThreadPoolExecutor pool = new ThreadPoolExecutor(engineThreads, engineThreads, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); try {/*w ww. j av a 2 s. co m*/ final List<Callable<String>> tasks = new LinkedList<>(); tasks.add(initTask); final List<Future<String>> futureList = pool.invokeAll(tasks); final Iterator<Future<String>> iterator = futureList.iterator(); if (iterator.hasNext()) { final Future<String> f = iterator.next(); final String initResult = f.get(); final String message1 = String.format("[%s][%d] initResult = '%s'", serviceName, cnt, initResult); LOG.info(message1); return initResult; } } catch (final Exception e) { LOG.error("[{]][{}] something went wrong at init part execution", serviceName, cnt, e); throw e; } finally { pool.shutdown(); } return null; }
From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java
/** * Generate data and upload to the data dir. * /*from w w w .j a v a2 s .c om*/ * @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 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.echopf.members.ECHOMemberQuery.java
/** * Does Login/*from w ww.ja v a 2 s . co m*/ * @param sync if set TRUE, then the main (UI) thread is waited for complete the logging-in in a background thread. * (a synchronous communication) * @param callback invoked after the logging-in is completed * @param instanceId the reference ID of the instance to which the logged-in member belong * @param login_id * @param password * @throws ECHOException */ protected static ECHOMemberObject doLogin(final boolean sync, final LoginCallback callback, final String instanceId, final String login_id, final String password) throws ECHOException { final Handler handler = new Handler(); // Get ready a background thread ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<ECHOMemberObject> communicator = new Callable<ECHOMemberObject>() { @Override public ECHOMemberObject call() throws ECHOException { ECHOException exception = null; ECHOMemberObject memberObj = null; try { JSONObject params = new JSONObject(); params.put("login_id", login_id); params.put("password", password); JSONObject response = ECHOQuery.postRequest(instanceId + "/login", params); memberObj = new ECHOMemberObject(instanceId, response.optString("refid"), response); // ECHO.accessToken = response.optString("access_token"); } catch (ECHOException e) { exception = e; } catch (Exception e) { exception = new ECHOException(e); } if (sync == false) { // Execute a callback method in the main (UI) thread. if (callback != null) { final ECHOException fException = exception; final ECHOMemberObject fMemberObj = memberObj; handler.post(new Runnable() { @Override public void run() { callback.done(fMemberObj, fException); } }); } return null; } else { if (exception == null) return memberObj; throw exception; } } }; Future<ECHOMemberObject> future = executor.submit(communicator); if (sync) { try { return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // ignore/reset } catch (ExecutionException e) { Throwable e2 = e.getCause(); if (e2 instanceof ECHOException) { throw (ECHOException) e2; } throw new RuntimeException(e2); } } return null; }