future

Can each Iteration of a for loop/for_each be done in parallel? (C++11)

巧了我就是萌 提交于 2019-12-22 04:05:14
问题 I'm iterating over a vector of structs and processing each struct individually. It looks something like this: for_each(begin(data),end(data),DoTask); //assume "data" is std::vector<DataT> //assume DoTask is a function that takes a DataT by reference The code is significantly slow because DoTask connects to particular websites and analyzes HTML. What would be the best way to speed this up? My goal is to analyze multiple DataTs at the same time. I'm very new to threading, but std::async and std

Whether method cancel() in java.util.concurrent.Future should be blocking?

五迷三道 提交于 2019-12-21 19:32:03
问题 I'm trying to implement Future<> interface in my project. But it looks like documentation is a little bit vague for it. From official documentation we can deduce: Method cancel() does not throw exceptions like InterruptedException or ExecutionException. Moreover it has no the variant with timeout. So it looks like, it should NOT be blocking. Documentation says After this method returns, subsequent calls to isDone() will always return true. but boolean isDone() Returns true if this task

Whether method cancel() in java.util.concurrent.Future should be blocking?

丶灬走出姿态 提交于 2019-12-21 19:31:28
问题 I'm trying to implement Future<> interface in my project. But it looks like documentation is a little bit vague for it. From official documentation we can deduce: Method cancel() does not throw exceptions like InterruptedException or ExecutionException. Moreover it has no the variant with timeout. So it looks like, it should NOT be blocking. Documentation says After this method returns, subsequent calls to isDone() will always return true. but boolean isDone() Returns true if this task

Dealing with failed futures

依然范特西╮ 提交于 2019-12-21 17:36:23
问题 In Play Framework 2.3, an action can produce a result from a successful future call like this: def index = Action.async { val futureInt = scala.concurrent.Future { intensiveComputation() } futureInt.map(i => Ok("Got result: " + i)) } But how can an action deal with a failed future call, i.e., a future that was completed by calling failure() instead of success() ? For instance, how could an action produce a InternalServerError result with the message returned in the future's failure's

Advantages of actors over futures

末鹿安然 提交于 2019-12-21 09:27:19
问题 I currently program in Futures, and I'm rather curious about actors. I'd like to hear from an experienced voice: What are the advantages of actors over futures? When should I use one instead of other? As far as I've read, actors hold state and futures doesn't, is this the only difference? So if I have true immutability I shouldn't care about actors? Please enlighten me :-) 回答1: One important difference is that actors typically have internal state, and therefore theoretically, they are not

Akka Future - Parallel versus Concurrent?

别来无恙 提交于 2019-12-21 05:08:19
问题 From the well-written Akka Concurrency: As I understand , the diagram points out, both numSummer and charConcat will run on the same thread. Is it possible to run each Future in parallel, i.e. on separate threads? 回答1: The picture on the left is them running in parallel. The point of the illustration is that the Future.apply method is what kicks off the execution, so if it doesn't happen until the first future's result is flatMap ed (as in the picture on the right), then you don't get the

Does the future object returned by executorService.submit(Runnable) hold any reference to the runnable object?

主宰稳场 提交于 2019-12-21 03:47:07
问题 Let's assume we have the following code: List<Future<?>> runningTasks; ExecutorService executor; ... void executeTask(Runnable task){ runningTasks.add(executor.submit(task)); } My questions are: Does runningTasks hold a reference to the task object? How long does it hold it for? Does it still hold it after the task is complete? In order to avoid memory leaks do I have to take care to remove the future that was added to the list? 回答1: Until when the executor or the Future object holds a

Waiting for callback for multiple futures

怎甘沉沦 提交于 2019-12-20 20:34:31
问题 Recently I've delved into a little bit of work using an API. The API uses the Unirest http library to simplify the work of receiving from the web. Naturally, since the data is called from the API server, I tried to be efficient by using asynchronous calls to the API. My idea is structured as follows: Create array of data by returning the results of futures Display data + additional information gathered from the data Therefore, I need to have all the data returned before I can start the second

Why does the andThen of Future not chain the result?

試著忘記壹切 提交于 2019-12-20 17:26:08
问题 The andThen meaning I learned from this answer is a function composer. Say that f andThen g andThen h will equal to h(g(f(x))) This implies the h function will receive input from g(f(x)) But for the andThen in Future , all the closure of the following andThen always receives the result from the original Future . Future{ 1 }.andThen{ case Success(x) => println(x) // print 1 Thread.sleep(2000) x * 2 }.andThen{ case Success(x) => println(x) // print 1 Thread.sleep(2000) x * 2 } compare to val

What is the behavior of scala.concurrent.ExecutionContext.Implicits.global?

纵饮孤独 提交于 2019-12-20 09:57:12
问题 The documentation for scala.concurrent.ExecutionContext.Implicits.global on the ExecutionContext trait reads: It is possible to simply import scala.concurrent.ExecutionContext.Implicits.global to obtain an implicit ExecutionContext . This global context is a reasonable default thread pool What does it mean by "reasonable default"? 回答1: Default It's a fixed size ThreadPool, which has as many threads as the processors on the machine. A reasonable default means it's good for most things most of