future

Most efficient way to stream on list of Futures

跟風遠走 提交于 2019-12-04 23:10:34
问题 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())

Flutter can't read from Clipboard

江枫思渺然 提交于 2019-12-04 22:23:31
I come asking for quite a specific question regarding Flutter and the Future and await mechanism, which seems to be working, but my Clipboard does not really function while operating with my editable text fields, even following Google's advice on implementation... This is my code for pasting: onPressed: () async { await getMyData('text'); _encodingController.text = clipData; Scaffold.of(context).showSnackBar( new SnackBar( content: new Text( "Pasted from Clipboard"), ), ); }, what doesnt work is my paste functionality... While debugging the result of this following function is null, wth???????

Flutter.wait() for multiple futures

为君一笑 提交于 2019-12-04 21:39:48
I'm trying to catch the error when my device has no internet connection. I've built out 2 future methods, 1 to import a json and 1 to look into the database. I have a future builder that's suppose to wait for both futures to finish before building out the grid view but it seems like the offlineFlashCardList is being prematurely called due to the connection error. Any idea how to make it wait for both futures to finish before the snapshot error gets called? import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; import 'package:baby_sound/strings.dart'; import 'package

java多线程编程之Future/FutureTask和Callable

夙愿已清 提交于 2019-12-04 20:25:32
有这样一种场景,用多线程发送数据到某个服务器,需要知道各个线程是否都发送成功,等所有线程都发送完成才能继续下一轮计算和发送。如果用传统的多线程方式,就需要启动多个线程,然后在每个线程中分别发送数据,外部通过某种方式等待各个线程全部都发送完成,再进行后面的计算等流程。这种实现方式的代码会比较臃肿,在java中提供了一种Callable+Future的方法,可以将异步的多线程调用变为同步方式。 Callable 在java的多线程编程中,有Thread和Runnable两种方式来新建线程,其中Runnable封装了一个异步运行的任务,可以认为是一个没有任何参数和返回值的异步方法。Callable接口类似于Runnable,两者都是为那些其实例可能被另一个线程执行的类设计的,不同之处在于: Runnable不会返回结果,并且无法抛出经过检查的异常。而Callable是有返回结果并且可能抛出异常的。 Runnable定义了run方法,而Callable定义了一个不带任何参数的叫做call的方法。 此外,Callable接口的类型参数也是返回值的类型。 public interface Callable { /** * Computes a result, or throws an exception if unable to do so. * * @return computed

Futures for blocking calls in Scala

耗尽温柔 提交于 2019-12-04 19:00:37
问题 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

Replacing std::async with own version but where should std::promise live?

雨燕双飞 提交于 2019-12-04 18:53:05
问题 I'm using vc2011 and it turns out the std::async(std::launch::async, ... ) is a bit buggy (sometimes it does not spawn new threads and runs them in parallel, but instead reuses threads and runs task one after another). This is too slow when I'm doing expensive network calls. So I figured I'd write my own async function. I'm getting stuck though, where should std::promise live? In the 1) thread function, 2) async function, or 3) caller function. Code: #include <future> #include <thread>

blocking keyword in Scala

风流意气都作罢 提交于 2019-12-04 17:54:22
问题 What's the difference between Future(blocking(blockingCall())) and blocking(Future(blockingCall())) ? Both of these are defined in scala.concurrent._ I've looked at the scala docs and some other stack overflow answers but remain unclear on what the difference is. 回答1: blocking acts as a hint to the ExecutionContext that it contains blocking code, so that it may spawn a new thread to prevent deadlocks. This presumes the ExecutionContext can do that, but not all are made to. Let's look at each

When using Scala futures, will chained callbacks with the same execution context be optimised into synchronous calls?

纵然是瞬间 提交于 2019-12-04 17:37:43
问题 The new Future in Scala 2.10 uses an execution context for every operation where an action is called asynchronously (including map , filter , etc). Does this mean that every action will always be called individually through the execution context, or is it possible that this step is optimized away when chaining multiple transformations/filters each using the same execution context? I.e. if doing f.map(...).filter(...).map(...) , all with the same execution context, will this call execute()

mapping a Stream with a function returning a Future

余生长醉 提交于 2019-12-04 16:48:15
问题 I sometimes find myself in a situation where I have some Stream[X] , and a function X => Future Y , that I'd like to combine to a Future[Stream[Y]] , and I can't seem to find a way to do it. For example, I have val x = (1 until 10).toStream def toFutureString(value : Integer) = Future(value toString) val result : Future[Stream[String]] = ??? I tried val result = Future.Traverse(x, toFutureString) which gives the correct result, but seems to consume the entire stream before returning the

JAVA线程12

两盒软妹~` 提交于 2019-12-04 15:57:58
一、概述 在Java5之前,线程是没有返回值的,要实现子线程完成任务后返回值给主线程需要借助第三方转存。 在JAVA5开始,有返回值的任务可以利用Callable接口来实现。 执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了。 二、示例 import java.util.concurrent.*; public class CallableDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { //创建一个线程池 ExecutorService pool = Executors.newSingleThreadExecutor(); //创建有返回值的任务 Callable callable = new MyCallable("zhangsan"); //执行任务并获取Future对象 Future future = pool.submit(callable); //从Future对象上获取任务的返回值,并输出到控制台 System.out.println("等待结果..."); System.out.println("得到结果:"+future.get()