future

What is the concrete type of a future returned from `async fn`?

巧了我就是萌 提交于 2020-03-15 05:44:08
问题 What type should I use for a vector that stores futures? I tried to make multiple concurrent requests on the same URL and save all the futures into the vector to use with join_all . If I don't set a type for the vector explicitly, everything works. I understand that Rust can find the proper type of a variable. CLion determines the vector type as Vec<dyn Future<Output = ()>> , but when I try to set the type by myself, it gives me an error: error[E0277]: the size for values of type `dyn core:

What is the concrete type of a future returned from `async fn`?

允我心安 提交于 2020-03-15 05:43:47
问题 What type should I use for a vector that stores futures? I tried to make multiple concurrent requests on the same URL and save all the futures into the vector to use with join_all . If I don't set a type for the vector explicitly, everything works. I understand that Rust can find the proper type of a variable. CLion determines the vector type as Vec<dyn Future<Output = ()>> , but when I try to set the type by myself, it gives me an error: error[E0277]: the size for values of type `dyn core:

FutureBuilder makes my app freeze because it waits for a file to load before building

喜夏-厌秋 提交于 2020-03-02 17:13:08
问题 I am writing a very basic flutter app for reading public domain books. I included a .txt file containing a book in my app's assets. since a book is quite long and would take time to load I was trying to use a FutureBuilder that will display a circular progress indicator while the book is loading, however, when I click on the button to open the book the app freezes until the book is loaded instead of transitioning to the book page and showing a progress indicator while the book is loading as I

Executor框架+实例

南笙酒味 提交于 2020-03-01 09:30:29
Executor框架 是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。 Executor框架简介 Executor框架描述 Executor框架 是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。 运用该框架能够很好的 将任务分成一个个的子任务 ,使并发编程变得方便。 Executor相关类图 该框架的类图(方法并没有都表示出来)如下: 创建线程池类别 创建线程池 Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。 public static ExecutorService newFixedThreadPool(int nThreads) 创建固定数目线程的线程池 public static ExecutorService newCachedThreadPool() 创建一个可缓存的线程池 创建一个可缓存的线程池,调用execute 将重用以前构造的线程

CompletionService小记

风流意气都作罢 提交于 2020-03-01 04:06:18
CompletionService小记 在使用ExecutorService时,通过sumit执行一个Callable的时候,会立即返回一个异步任务结果,然后通过get获取异步任务结果的时候会阻塞,如下面这种情况,代码如下, import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** * Created by xinxingegeya on 16/3/22. */ public class ExecutorServiceTest { public static void main(String args[]) throws ExecutionException, InterruptedException { ExecutorService

线程池原理及应用

六月ゝ 毕业季﹏ 提交于 2020-02-29 01:37:35
一、线程池的创建和常用参数分析 先看一个线程池的创建 private static void testThreadPool ( ) { ExecutorService executorService = Executors . newFixedThreadPool ( 1 ) ; Future < ? > future = executorService . submit ( ( ) - > { System . out . println ( Thread . currentThread ( ) . getName ( ) + " running" ) ; } ) ; } 翻看newFixedThreadPool()的源码可以看到最终执行的时候有以下几个参数: public ThreadPoolExecutor ( int corePoolSize , int maximumPoolSize , long keepAliveTime , TimeUnit unit , BlockingQueue < Runnable > workQueue , ThreadFactory threadFactory , RejectedExecutionHandler handler ) { . . . } a) corePoolSize 核心线程数,保持在线程池中线程的数量 b)

java并发callable,runnable,future和futureTask

假如想象 提交于 2020-02-28 21:10:10
java多线程 java实现多线程有两种方式:一个是直接继承Thread类,一种是实现Runnable接口。但这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果。于是后面又增加了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果。 callable与runnable java.lang.Runnable是一个接口,在它里面只声明了一个run()方法: public interface Runnable { public abstract void run(); } 由于run()方法返回值为void类型,所以在执行完任务之后无法返回任何结果。   Callable位于java.util.concurrent包下,它也是一个接口,在它里面也只声明了一个方法,只不过这个方法叫做call(): public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; } 可以看到,这是一个泛型接口,call(

多线程IO操作(扫描文件夹并计算总大小)

走远了吗. 提交于 2020-02-28 11:55:02
场景为,给到一个硬盘上文件或文件夹,(当然文件夹时,多线程的优势才能越发体现出来),得到该文件或文件夹的大小和计算该结果所需要的时间。 首先是单线程下的例子,这个可难不倒大家,代码如下: public class TotalFileSizeSequential { private long getTotalSizeOfFilesInDir(final File file) { if (file.isFile()) return file.length(); final File[] children = file.listFiles(); long total = 0; if (children != null) for(final File child : children) total += getTotalSizeOfFilesInDir(child); return total; } public static void main(final String[] args) { final long start = System.nanoTime(); final long total = new TotalFileSizeSequential() .getTotalSizeOfFilesInDir(new File("D:/idea_ws")); final long

Dart, how to create a future to return in your own functions?

混江龙づ霸主 提交于 2020-02-17 18:02:39
问题 is it possible to create your own futures in Dart to return from your methods, or must you always return a built in future return from one of the dart async libraries methods? I want to define a function which always returns a Future<List<Base>> whether its actually doing an async call (file read/ajax/etc) or just getting a local variable, as below: List<Base> aListOfItems = ...; Future<List<Base>> GetItemList(){ return new Future(aListOfItems); } 回答1: If you need to create a future, you can

How to read only Successful values from a Seq of Futures

两盒软妹~` 提交于 2020-02-06 04:10:07
问题 I am learning akka/scala and am trying to read only those Future s that succeeded from a Seq[Future[Int]] but cant get anything to work. I simulated an array of 10 Future[Int] some of which fail depending on the value FailThreshold takes (all fail for 10 and none fail for 0). I then try to read them into an ArrayBuffer (could not find a way to return immutable structure with the values). Also, there isn't a filter on Success/Failure so had to run an onComplete on each future and update buffer