future

Callable与Future

限于喜欢 提交于 2019-12-01 06:22:15
接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果。 Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,这个返回值可以被Future拿到,也就是说,Future可以拿到异步执行任务的返回值,下面来看一个简单的例子: public class CallableAndFuture { public static void main(String[] args) { Callable<Integer> callable = new Callable<Integer>() { public Integer call() throws Exception { return new Random().nextInt(100); } }; FutureTask<Integer> future = new FutureTask<Integer>(callable); new Thread(future).start(); try { Thread.sleep(5000);// 可能做一些事情 System.out.println(future.get()); } catch

Using loops with Futures in Dart

喜欢而已 提交于 2019-12-01 05:44:28
Okay, so I have a List of Files and I need to run a function on each member of the list. I essentially want to do something like this: for(File file in files) { functionThatReturnsAFuture(file); } But obviously this won't work, since the function that returns a Future fires of asynchronously. Is my only option something like this? List<File> files = new List<File>(); // Add files somewhere Future processFile(int i) { return new Future.sync(() { //Do stuff to the file if(files.length>i+1) { return processFile(i+1); } }); } processFile(0); EDIT: I suppose more context is important. The end goal

How to implement Future as Applicative in Scala?

百般思念 提交于 2019-12-01 04:49:45
Suppose I need to run two concurrent computations, wait for both of them, and then combine their results. More specifically, I need to run f1: X1 => Y1 and f2: X2 => Y2 concurrently and then call f: (Y1, Y2) => Y to finally get a value of Y . I can create future computations fut1: X1 => Future[Y1] and fut2: X2 => Future[Y2] and then compose them to get fut: (X1, X2) => Future[Y] using monadic composition. The problem is that monadic composition implies sequential wait . In our case it implies that we wait for one future first and then we will wait for another. For instance. if it takes 2 sec.

Chaining difference in ES6 Promises and PEP3148 Futures

倖福魔咒の 提交于 2019-12-01 04:46:02
I am somewhat puzzled about reasoning in difference in implementation of ES6 Promises and PEP3148 Futures. In Javascript, when Promise is resolved with another Promise, "outer" promise inherits the value of "inner" promise once it's resolved or rejected. In Python, "outer" future is instead immediately resolved with "inner" future itself, not with it's eventual value, and that is the problem. To illustrate this, I had provided two code snippets for both platforms. In Python, code looks like this: import asyncio async def foo(): return asyncio.sleep(delay=2, result=42) async def bar(): return

Initializing an actor before being able to handle some other messages

泄露秘密 提交于 2019-12-01 04:10:59
I have an actor which creates another one: class MyActor1 extends Actor { val a2 = system actorOf Props(new MyActor(123)) } The second actor must initialize (bootstrap) itself once it created and only after that it must be able to do other job. class MyActor2(a: Int) extends Actor { //initialized (bootstrapped) itself, potentially a long operation //how? val initValue = // get from a server //handle incoming messages def receive = { case "job1" => // do some job but after it's initialized (bootstrapped) itself } } So the very first thing MyActor2 must do is do some job of initializing itself.

Future.wait() can't wait without a fiber (while waiting on another future in Meteor.method)

不想你离开。 提交于 2019-12-01 04:07:42
In Meteor , I'm writing a method that will have to check a certain path's subdirectories for new files. I first would like to just list the subdirectories within Meteor after which I child_process.exec a simple bash script that lists files added since the last time it executed. I'm having some issues getting the directory discovery to be async ( Error: Can't wait without a fiber ). I've written a synchronous version but having both fs.readdir and fs.stat in stead of their synchronous alternatives allows me to catch errors. Here's the code: function listDirs(dir, isDir){ var future1 = new

Flutter Isolate vs Future

若如初见. 提交于 2019-12-01 03:40:49
I might get the wrong idea of Isolate and Future, please help me to clear it up. Here are my understanding of both subjects. Isolate: Isolates run code in its own event loop, and each event may run smaller tasks in a nested microtask queue. Future: A Future is used to represent a potential value, or error, that will be available at some time in the future. My confusions are: The doc says Isolate has it own loop? I feel like having its own event queue makes more sense to me, am I wrong? Is future running asynchronously on the main Isolate? Im assuming future task actually got placed at the end

Waiting for a cancelled future to actually finish

蓝咒 提交于 2019-12-01 02:47:05
问题 I have a SwingWorker which calls some code that does not check for thread interruption. After the call to worker.cancel(true) , the worker.get() method will throw CancellationException immediately (as it is supposed to). However, since the background task's code never checks for its thread to be interrupted, it happily continues executing. Is there a standard way to wait for the background task to actually finish? I'm looking to show a "Cancelling..." message or something of the sort and

Chaining difference in ES6 Promises and PEP3148 Futures

て烟熏妆下的殇ゞ 提交于 2019-12-01 02:09:03
问题 I am somewhat puzzled about reasoning in difference in implementation of ES6 Promises and PEP3148 Futures. In Javascript, when Promise is resolved with another Promise, "outer" promise inherits the value of "inner" promise once it's resolved or rejected. In Python, "outer" future is instead immediately resolved with "inner" future itself, not with it's eventual value, and that is the problem. To illustrate this, I had provided two code snippets for both platforms. In Python, code looks like

C++ 11 future_status::deferred not working

青春壹個敷衍的年華 提交于 2019-12-01 01:41:47
问题 #include <iostream> #include <future> #include <chrono> using namespace std; using namespace std::chrono; int sampleFunction(int a) { return a; } int main() { future<int> f1=async(launch::deferred,sampleFunction,10); future_status statusF1=f1.wait_for(seconds(10)); if(statusF1==future_status::ready) cout<<"Future is ready"<<endl; else if (statusF1==future_status::timeout) cout<<"Timeout occurred"<<endl; else if (statusF1==future_status::deferred) cout<<"Task is deferred"<<endl; cout<<"Value :