future

Akka Future - Parallel versus Concurrent?

孤街醉人 提交于 2019-12-03 15:48:16
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? 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 parallel execution. (Note that by "kicked off", i mean the relevant ExecutionContext is told about the job.

Most efficient way to stream on list of Futures

孤者浪人 提交于 2019-12-03 15:02:30
I'm calling an async client method by streaming over a list of objects. The method returns Future. What's the best way to iterate over the list of Futures returned after the call (so as to process those Future which comes first)? Note: The async client only returns Future not CompletableFuture. Following is the code: List<Future<Object>> listOfFuture = objectsToProcess.parallelStream() .map((object) -> { /* calling an async client returning a Future<Object> */ }) .collect(Collectors.toList()); Eugene Having this list of List<Future<Object>> , I would submit it to a custom pool, instead of

What is the difference between “Future.successful(None)” and “Future(None)”

╄→гoц情女王★ 提交于 2019-12-03 14:31:26
问题 Future.apply starts an asynchronous computation whereas Future.successful creates an already completed Future with the specified result. Now is Future(None) ( Future.apply(None) ) less efficient than Future.successful(None) ? 回答1: Future.apply(None) creates asynchronous computation and executes it. It means that extra lambda object is created and extra task is scheduled (however trivial task). Future.successful(None) just produces already completed future. It is more efficient. 回答2: I don't

Multithreaded search operation

时光怂恿深爱的人放手 提交于 2019-12-03 14:04:51
问题 I have a method that takes an array of queries, and I need to run them against different search engine Web API's, such as Google's or Yahoo's. In order to parallelize the process, a thread is spawned for each query, which are then joined at the end, since my application can only continue after I have the results of every query. I currently have something along these lines: public abstract class class Query extends Thread { private String query; public abstract Result[] querySearchEngine();

Play 2.2 -Scala - How to chain Futures in Controller Action

最后都变了- 提交于 2019-12-03 13:42:56
问题 I have 3 futures of type Response. The first future returns a JsValue which defines if future 2 and future 3 shall be executed or only future 3 shall be executed. Pseudocode: If Future 1 then {future2 and future 3} else future 3 Iam trying to do this in a play framwork action which means in order to use the result of the futures later I cant use onSuccess, onFailure and onComplete because all of them return Unit and not the actual JsValue from the last future. I tried to do this with map()

Are Futures executed on a single thread? (Scala)

≡放荡痞女 提交于 2019-12-03 12:42:21
问题 Using the default implicit execution context in Scala, will each new future be computed on a single, dedicated thread or will the computation be divided up and distributed to multiple threads in the thread pool? I don't know if this helps, the background to this question is that I want to perform multiple concurrent operations using the HtmlUnit API. To do this, I would wrap each new WebClient instance in a Future. The only problem is that the WebClient class is not thread safe, so I'm

Futures for blocking calls in Scala

别等时光非礼了梦想. 提交于 2019-12-03 12:38:29
The Akka documentation says: you may be tempted to just wrap the blocking call inside a Future and work with that instead, but this strategy is too simple: you are quite likely to find bottlenecks or run out of memory or threads when the application runs under increased load. They suggest the following strategies: Do the blocking call within a Future , ensuring an upper bound on the number of such calls at any point in time (submitting an unbounded number of tasks of this nature will exhaust your memory or thread limits). Do the blocking call within a Future , providing a thread pool with an

Storing a future in a list

∥☆過路亽.° 提交于 2019-12-03 12:35:56
I want to store the futures of several threads spawned using async in a list to retrieve their results later. future<int> f = async(doLater, parameter); list<future<int>> l; l.push_back(f); However the compiler prints the following error message /usr/include/c++/4.7/bits/stl_list.h:115:71: error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = int; std::future<_Res> = std::future]' Am i doing something wrong or aren't lists supposed to store futures? If they are not, what to use instead? std::future is not copyable - you need to move into the list.

Using futures and Thread.sleep

霸气de小男生 提交于 2019-12-03 12:29:58
问题 By executing this scala code, I don't have any output in the console. (I don't really understand what is happening) If I remove Console.println("Console.println OK!") => everything seems fine. If I remove Thread.sleep(2000) => everything seems fine. Do you have any ideas about this ? Thank you very much! Clément import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.language.postfixOps object

Callable,Future,FutrueTask,CompletionService 详解

醉酒当歌 提交于 2019-12-03 12:15:46
package com.dy.pool; import java.util.concurrent.*; /** * 1: Callable<V> 返回结果并且可能抛出异常的任务。实现者定义了一个不带任何参数的叫做 call 的方法。 Callable 接口类似于 Runnable,两者都是为那些其实例可能被另一个线程执行的类设计的。 但是 Runnable 不会返回结果,并且无法抛出经过检查的异常。 2: Future<V> 表示异步计算的结果 Future 表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果。 3: CompletionService<V> 将生产新的异步任务与使用已完成任务的结果分离开来的服务 , * 生产者 submit 执行的任务。使用者 take 已完成的任务,并按照完成这些任务的顺序处理它们的结果。 * * * 4:FutureTask<V> 可取消的异步计算。实现了RunbaleFuture, * * RunbaleFuture 继承了Future和Runbale,所以FutureTask<V>可以当做一个线程去提交或执行。 * * 返回FutureTask的操作也可以用Future来接受 * * 利用开始和取消计算的方法、查询计算是否完成的方法和获取计算结果的方法,此类提供了对 Future 的基本实现。 * *