future

Exception during Callable execution

被刻印的时光 ゝ 提交于 2019-12-05 10:00:39
I have the following Callable: public class Worker implements Callable<Boolean>{ @Override public Boolean call(){ boolean success=true; //do Something return success; } } Now I'm executing it: Worker worker - new Worker(); Future<Boolean> submit = executor.submit(worker); I'm storing the submit in kind of hashMap for some operation to be performed somewhere in code. How can I know if any exception has occured in worker.call() function? Will submit.isCancelled() return true if some sort of Exception occurred and false if everything works ok? Victor Sorokin When you call Future.get() it will

Why does this list-of-futures to future-of-list transformation compile and work?

a 夏天 提交于 2019-12-05 08:03:11
Disclaimer: the code snippet below relates to one of ongoing Coursera courses. Let's consider it's posted just for a learning purpose and should not be used for submitting as a solution for one's homework assignment. As the comment below states, we need to transform a list of Futures to a single Future of a list. More than that, the resulting Future should fail if at least one of input futures failed. I met the following implementation and I don't understand it completely. /** Given a list of futures `fs`, returns the future holding the list of values of all the futures from `fs`. * The

Turning a listener into a future in java

旧城冷巷雨未停 提交于 2019-12-05 07:54:05
I'm trying to turn a listener into a Future, for asynchronous connection. I'm not used to using java futures yet, I've some experience with javascript promises but I fail to see how to write it in java (I've seen "CompletableFuture" in Java 8 may solve my problem, unfortunately I'm stuck with java 7). Here's what I've done so far: public Future<Boolean> checkEmailClientConfiguration(final EmailClientConfiguration config) { final Future<Boolean> future = ???; // In some other languages I would create a deferred Transport transport = null; try { transport = session.getTransport("smtp");

Who is responsible for the shared state of futures and promises

久未见 提交于 2019-12-05 05:16:14
Who owns the shared state in futures and promises? In particular who is responsible for the construction and deletion of the shared state in these classes? Or is the shared state supposed to be reference counted? I am not able to get an answer by reading the docs for these on cppreference. The way I was thinking about it the easiest thing to do would be to have the std::promise class be responsible for the creation of the shared state, but then hand it off to the std::future that is fetched from the std::promise for deletion when the future is destroyed. But then this approach can lead to

Does a wait on Scala Future block thread?

风流意气都作罢 提交于 2019-12-05 04:14:20
When I wait for result of Scala Future, does it behave more like receive , or like react , i.e. does it block a thread, or schedules a continuation after result if available? Vasil Remeniuk Yes, in stdlib it blocks the thread, and synchronously waits for results. If you want to apply continuation-passing style to futures, you'd have to use Akka or Scalaz that allow adding hooks on futures completion straight from the box. Akka : val f1 = Future { Thread.sleep(1000); "Hello" + "World" } val f2 = f1 map { _.length } f2 foreach println //Done asynchronously and non-blocking Same with Scalaz :

Wait for an unknown number of futures

拥有回忆 提交于 2019-12-05 02:55:32
In Scala 2.10, what is a correct way to write a function that returns a future which completes when all futures in a list complete? After researching and experimenting, I have developed the code below, in a Scala Worksheet: import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration._ import scala.concurrent._ object ws2 { def executeFutures(futures: Seq[Future[Unit]]): Future[Unit] = { def cascadeFutures(futureSeq: Seq[Future[Unit]], f: Future[Unit]): Future[Unit] = { futureSeq match { case h :: t => h.flatMap { u => cascadeFutures

Sequentially combine arbitrary number of futures in Scala

∥☆過路亽.° 提交于 2019-12-05 02:38:27
I'm new to scala and I try to combine several Futures in scala 2.10RC3. The Futures should be executed in sequential order. In the document Scala SIP14 the method andThen is defined in order to execute Futures in sequential order. I used this method to combine several Futures (see example below). My expectation was that it prints 6 but actually the result is 0 . What am I doing wrong here? I have two questions: First, why is the result 0 . Second, how can I combine several Futures , so that the execution of the second Future does not start before the first Future has been finished. val intList

Unit test succeeds in debug mode but fails when running it normally

谁说胖子不能爱 提交于 2019-12-05 01:59:37
Why does my unit test succeed in debug mode but fail when running it normally? public class ExecutorServiceTest extends MockitoTestCase{ private int numThreads; private ExecutorService pool; private volatile boolean interruptedBitSet; @Override public void setUp() { numThreads = 5; pool = Executors.newFixedThreadPool(numThreads); } class TaskChecksForInterruptedBit implements Callable<String> { @Override public String call() throws Exception { interruptedBitSet = false; while (!Thread.currentThread().isInterrupted()) { } interruptedBitSet = Thread.currentThread().isInterrupted(); return "blah"

Exception propagation and std::future

℡╲_俬逩灬. 提交于 2019-12-05 01:03:33
My understanding is that when an asynchronous operation throws an exception, it will be propagated back to a thread that calls std::future::get() . However, when such a thread calls std::future::wait() , the exception is not immediately propagated - it'll be thrown upon a subsequent call to std::future::get() . However, In such a scenario, what is supposed to happen to such an exception if the future object goes out of scope after a call to std::future::wait() , but prior to a call to std::future::get() ? For those interested, here is a simple example. In this case, the exception is silently

Should I return CompletableFuture or Future when defining API?

99封情书 提交于 2019-12-05 00:49:41
In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture instead of returning Future ? Considering it is ugly converting Future to CompletableFuture and the fact that CompletableFuture will give the caller more flexibility of using functional style directly, what could be a good reason for an API to just return Future ? My 2 cts: by returning a Future, you keep your options open and can return a Future, or a CompletableFuture - it makes no difference from the caller's perspective. by returning a CompletableFuture, you give the caller more options (they