List of usage examples for java.util.concurrent Future isDone
boolean isDone();
From source file:bear.core.AbstractConsole.java
public void stopStreamCopiers() { // logger.debug("OOOOOOOOOOOOPS - stopStreamCopiers", new Exception()); for (int i = 0; i < copiers.size(); i++) { copiers.get(i).stop();// w w w . j av a 2s. c om final Future future = futures.get(i); if (!future.isDone()) { future.cancel(true); } } }
From source file:aos.camel.JavaFutureTest.java
@Test public void testFutureWithDone() throws Exception { // this is the task we want to execute async // usually the task is something that takes // some time to do Callable<String> task = new Callable<String>() { public String call() throws Exception { // do something that takes some time LOG.info("Starting to process task"); Thread.sleep(5000);/*from ww w . ja va 2 s. com*/ LOG.info("Task is now done"); return "Camel rocks"; } }; // this is the thread pool we will use ExecutorService executor = Executors.newCachedThreadPool(); // now submit the task to the thread pool // and get the Future handle back so we can later get the result LOG.info("Submitting task to ExecutorService"); Future<String> future = executor.submit(task); LOG.info("Task submitted and we got a Future handle"); // test when we are done boolean done = false; while (!done) { done = future.isDone(); LOG.info("Is the task done? " + done); if (!done) { Thread.sleep(2000); } } // and get the answer String answer = future.get(); LOG.info("The answer is: " + answer); }
From source file:com.msopentech.odatajclient.engine.it.AsyncTestITCase.java
@Test public void retrieveEntitySet() throws InterruptedException, ExecutionException { final URIBuilder uriBuilder = client.getURIBuilder(testDefaultServiceRootURL) .appendEntitySetSegment("Product"); final Future<ODataRetrieveResponse<ODataEntitySet>> futureRes = client.getRetrieveRequestFactory() .getEntitySetRequest(uriBuilder.build()).asyncExecute(); assertNotNull(futureRes);/*from w ww .j av a 2 s.c o m*/ while (!futureRes.isDone()) { } final ODataRetrieveResponse<ODataEntitySet> res = futureRes.get(); assertNotNull(res); assertEquals(200, res.getStatusCode()); assertFalse(res.getBody().getEntities().isEmpty()); }
From source file:org.apache.olingo.fit.proxy.AsyncTestITCase.java
@Test public void invoke() throws Exception { final ProductCollectionComposableInvoker invoker1 = container.operations().getAllProducts(); final Future<ProductCollection> future1 = invoker1.operations().discount(10).filter("Name eq XXXX") .select("Name", "ProductDetail").expand("ProductDetail").orderBy("Name").skip(3).top(5) .executeAsync();/*from w w w. ja v a 2 s. c o m*/ while (!future1.isDone()) { Thread.sleep(1000L); } assertFalse(future1.get().isEmpty()); final PersonComposableInvoker invoker2 = container.operations().getPerson2("London"); final Future<Person> future2 = invoker2.select("Name").expand("Order").executeAsync(); while (!future2.isDone()) { Thread.sleep(1000L); } assertNotNull(future2.get()); }
From source file:org.apache.olingo.fit.v3.AsyncTestITCase.java
@Test public void retrieveEntitySet() throws InterruptedException, ExecutionException { final URIBuilder uriBuilder = client.newURIBuilder(testStaticServiceRootURL) .appendEntitySetSegment("Product"); final Future<ODataRetrieveResponse<ODataEntitySet>> futureRes = client.getRetrieveRequestFactory() .getEntitySetRequest(uriBuilder.build()).asyncExecute(); assertNotNull(futureRes);/*from www. j a v a2 s . c o m*/ while (!futureRes.isDone()) { Thread.sleep(1000L); } final ODataRetrieveResponse<ODataEntitySet> res = futureRes.get(); assertNotNull(res); assertEquals(200, res.getStatusCode()); assertFalse(res.getBody().getEntities().isEmpty()); }
From source file:org.apache.olingo.client.core.it.v3.AsyncTestITCase.java
@Test public void retrieveEntitySet() throws InterruptedException, ExecutionException { final CommonURIBuilder<?> uriBuilder = client.getURIBuilder(testStaticServiceRootURL) .appendEntitySetSegment("Product"); final Future<ODataRetrieveResponse<ODataEntitySet>> futureRes = client.getRetrieveRequestFactory() .getEntitySetRequest(uriBuilder.build()).asyncExecute(); assertNotNull(futureRes);// w w w . j a v a 2 s . c o m while (!futureRes.isDone()) { Thread.sleep(1000L); } final ODataRetrieveResponse<ODataEntitySet> res = futureRes.get(); assertNotNull(res); assertEquals(200, res.getStatusCode()); assertFalse(res.getBody().getEntities().isEmpty()); }
From source file:org.yamj.filescanner.service.LibrarySendScheduler.java
private boolean checkStatus(Library library, Future<StatusType> statusType, String directory) throws InterruptedException, ExecutionException { boolean sendStatus; if (statusType.isDone()) { StatusType processingStatus = statusType.get(); if (processingStatus == StatusType.NEW) { LOG.info(" Sending '{}' to core for processing.", directory); sendStatus = sendToCore(library, directory); } else if (processingStatus == StatusType.UPDATED) { LOG.info(" Sending updated '{}' to core for processing.", directory); sendStatus = sendToCore(library, directory); } else if (processingStatus == StatusType.ERROR) { LOG.info(" Resending '{}' to core for processing (was in error status).", directory); sendStatus = sendToCore(library, directory); } else if (processingStatus == StatusType.DONE) { LOG.info(" Completed: '{}'", directory); sendStatus = Boolean.TRUE; } else {/*from ww w .ja va 2 s . c o m*/ LOG.warn(" Unknown processing status {} for {}", processingStatus, directory); // Assume this is correct, so we don't get stuck sendStatus = Boolean.TRUE; } } else { LOG.warn(" Still being procesed {}", directory); sendStatus = Boolean.FALSE; } return sendStatus; }
From source file:com.mozilla.fhr.consumer.FHRConsumer.java
@Override public void poll() { final CountDownLatch latch = new CountDownLatch(streams.size()); workers = new ArrayList<Future<Void>>(streams.size()); for (final KafkaStream<Message> stream : streams) { workers.add(executor.submit(new FHRConsumerWorker(stream, latch))); }// w ww. j a v a 2 s . c o m // Wait for all tasks to complete which in the normal case they will // run indefinitely unless killed try { while (true) { latch.await(10, TimeUnit.SECONDS); if (latch.getCount() != streams.size()) { // we have a dead thread and should exit break; } } } catch (InterruptedException e) { LOG.info("Interrupted during polling", e); } // Spit out errors if there were any for (Future<Void> worker : workers) { try { if (worker.isDone() && !worker.isCancelled()) { worker.get(1, TimeUnit.SECONDS); } } catch (InterruptedException e) { LOG.error("Thread was interrupted:", e); } catch (ExecutionException e) { LOG.error("Exception occured in thread:", e); } catch (TimeoutException e) { LOG.error("Timed out waiting for thread result:", e); } catch (CancellationException e) { LOG.error("Thread has been canceled: ", e); } } }
From source file:com.adaptris.core.services.splitter.ServiceWorkerPool.java
private List<Worker> waitFor(List<Future<Worker>> tasks) throws Exception { List<Worker> result = new ArrayList<>(); do {/*ww w. j a v a 2 s . c om*/ for (Iterator<Future<Worker>> i = tasks.iterator(); i.hasNext();) { Future<Worker> f = i.next(); if (f.isDone()) { result.add(f.get()); i.remove(); } } if (tasks.size() > 0) { LifecycleHelper.waitQuietly(100); } } while (tasks.size() > 0); return result; }
From source file:org.apache.streams.mongo.MongoPersistReader.java
@Override public void startStream() { LOGGER.debug("startStream"); MongoPersistReaderTask readerTask = new MongoPersistReaderTask(this); Thread readerTaskThread = new Thread(readerTask); Future future = executor.submit(readerTaskThread); while (!future.isDone() && !future.isCancelled()) { try {// ww w. j a v a 2s. c om Thread.sleep(1000); } catch (InterruptedException interrupt) { LOGGER.trace("Interrupt", interrupt); } } executor.shutdown(); }