future

Waiting for another future to end to return a function

浪子不回头ぞ 提交于 2019-12-24 03:46:06
问题 Let's say I have a function func1 that needs to return a Future with two integers. Each of the two values are returned by independent futures, like so: def f1 = Future { 1 } def f2 = Future { 2 } def func1 : Future[(Int,Int)] = { val future1 = f1 future1.map { result1 => result1 * 10 } val future2 = f2 future2.map { result2 => result2 * 20 } } I need future1 wait until future2 ends (or vice versa) to return both results as (Int,Int) . How can this be accomplished? 回答1: That's precisely what

Can a ListenableFuture chain handle inner ExecutionException?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 03:08:15
问题 I am provided with an api ( fnGrpc ) that performs a gRPC call and returns a ListenableFuture that resolves to some value v (the implementation of which is fixed and unmodifiable). I want to provide a helper function ( fnHelper ) that: does some transformational processing on the gRPC result and itself returns a ListenableFuture that resolves to the transformed value t1 . handles failure of the gRPC call, and returns some other value t2 instead of having fnHelper 's caller see an

Can a ListenableFuture chain handle inner ExecutionException?

穿精又带淫゛_ 提交于 2019-12-24 03:08:13
问题 I am provided with an api ( fnGrpc ) that performs a gRPC call and returns a ListenableFuture that resolves to some value v (the implementation of which is fixed and unmodifiable). I want to provide a helper function ( fnHelper ) that: does some transformational processing on the gRPC result and itself returns a ListenableFuture that resolves to the transformed value t1 . handles failure of the gRPC call, and returns some other value t2 instead of having fnHelper 's caller see an

Scala - Future List first completed with condition

风流意气都作罢 提交于 2019-12-23 19:44:53
问题 I have a list of Futures and I want to get the first one completed with a certain condition. Here is an example of a possible code: val futureList: Future[List[T]] = l map (c => c.functionWithFuture()) val data = for { c <- futureList }yield c data onSuccess { case x => (x filter (d=> d.condition)).head } But it's not efficient, because I'll take only one element of the list, but computed all of them with a lot of latency. I know firstCompletedOf but it's not what I'm searching. (Sorry for my

Flutter Future<bool> vs bool type

喜你入骨 提交于 2019-12-23 19:20:34
问题 My Flutter project has a utility.dart file and a main.dart file. I call the functions in the main.dart file but it has problems. It always showAlert "OK", i think the problem is the the utility class checkConnection() returns a future bool type. main.dart: if (Utility.checkConnection()==false) { Utility.showAlert(context, "internet needed"); } else { Utility.showAlert(context, "OK"); } utility.dart: import 'package:flutter/material.dart'; import 'package:connectivity/connectivity.dart';

How to move future into lambda-expression

喜欢而已 提交于 2019-12-23 18:23:01
问题 I'm using Visual Studio 2013 and i want achieve this line of code f = p.get_future(); auto task =[f = std::move(f)](){ //use f }; I'm aware of the solution here, but unfortunately this doesn't compile under VS2013 ( error C2558 no copy-constructor available ). 回答1: You can use a shared_future . That is easiest. However, that doesn't help you move. If you really need to move, we can do it with the help of a move_helper function and class: template<class T, class F=void> struct move_helper_t {

Scala - futures does not run

感情迁移 提交于 2019-12-23 16:54:00
问题 I am trying to run the following future basic code future { println("ssss")} onSuccess{ case _ => println("succ")} However, when I run the main method, nothing to the console is printed and the system exits almost instantly. I am using the implicit ExecutionContext. Any hints? This code: val f = future(Await.ready(Promise().future, d.timeLeft)) f.onSuccess { case _ => println("hee") } also exits immediately.... 回答1: Futures are executed on a dedicated thread pool. If your main program does

implementing PriorityQueue on ThreadPoolExecutor

二次信任 提交于 2019-12-23 16:21:25
问题 Been struggling with this for over 2 days now. implemented the answer I saw on here Specify task order execution in Java public class PriorityExecutor extends ThreadPoolExecutor { public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } //Utitlity method to create thread pool easily public static ExecutorService newFixedThreadPool(int nThreads)

Using mapTo with futures in Akka/Scala

雨燕双飞 提交于 2019-12-23 09:29:52
问题 I've recently started coding with Akka/Scala, and I've run into the following problem: With a implicit conversion in scope, such as: implicit def convertTypeAtoTypeX(a: TypeA): TypeX = TypeX() // just some kinda conversion This works: returnsAFuture.mapTo[TypeX].map { x => ... } But this doesn't: returnsAFuture.mapTo[TypeX].onComplete { ... } The latter fails with a type cast exception. (i.e. TypeA cannot be cast to TypeX) Very confused. Why? I suspect it has something to do with Try, but I

Why calling get() before exceptional completion waits for exceptionally to execute?

你。 提交于 2019-12-23 09:15:11
问题 While answering this question, I noticed a strange behaviour of CompletableFuture : if you have a CompletableFuture cf and chain a call with cf.exceptionally() , calling cf.get() appears to behave strangely: if you call it before exceptional completion, it waits for the execution of the exceptionally() block before returning otherwise, it fails immediately by throwing the expected ExecutionException Am I missing something or is this a bug? I am using Oracle JDK 1.8.0_131 on Ubuntu 17.04. The