future

scala.concurrent.Future wrapper for java.util.concurrent.Future

末鹿安然 提交于 2019-11-27 22:46:23
I'm using Play Framework 2.1.1 with an external java library that produces a java.util.concurrent.Future result. I'm using the scala future's as opposed to Akka which I think is the right thing to do as of Play 2.1. How can I wrap the java.util.concurrent.Future up into a scala.concurrent.Future while still keeping the code non-blocking? def geConnection() : Connection = { // blocking with get connectionPool.getConnectionAsync().get(30000, TimeUnit.MILLISECONDS) } The above code returns a connection but uses a get so it becomes blocking def getConnectionFuture() : Future[Connection] = { future

多线程(一) 创建多线程

血红的双手。 提交于 2019-11-27 21:31:41
1.继承Thread,重新run方法 2.实现Runnable方法 3.FutureTask实现Future public void testCallBack() { final FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { int i = 0; while (i < 100) { i++; } return i+""; } }); new Thread(futureTask).start(); //FutureTask实现了Callbale和Runnable接口 boolean done = futureTask.isDone();//如果Task完成,返回true boolean cancelled = futureTask.isCancelled();//正常完成前被取消,返回true futureTask.cancel(true); //试图取消Future关联的Callable任务 try { futureTask.get(2000, TimeUnit.SECONDS);//阻塞方法,超过该时间没有返回值则报错 } catch (ExecutionException e)

race-condition in pthread_once()?

陌路散爱 提交于 2019-11-27 20:12:16
I have a std::future in one thread which is waiting on a std::promise being set in another thread. EDIT: Updated the question with an exemplar app which will block forever: UPDATE: If I use a pthread_barrier instead, the below code does not block. I have created a test-app which illustrates this: Very basically class foo creates a thread which sets a promise in its run function, and waits in the constructor for that promise to be set. Once set, it increments an atomic count I then create a bunch of these foo objects, tear them down, and then check my count . #include <iostream> #include

Js Deferred/Promise/Future compared to functional languages like Scala

折月煮酒 提交于 2019-11-27 19:25:08
I'm mostly using programming languages like Scala and JavaScript. I'm trying to understand the similarities and differences in how async reactive programming is used in both languages. Can you help me? I'm not taking any particular Js Promise framework because it seems many implement the similar specifications (like Promise/A). I've only used Q so far. It seems that in Javascript we call a Deferred the object we resolve to complete a Promise . In Scala, it seems the Promise is the object you resolve to get a Future monad. Can someone tell me if this is right? Is there any good reason for a

Combine awaitables like Promise.all

陌路散爱 提交于 2019-11-27 19:16:36
In asynchronous JavaScript, it is easy to run tasks in parallel and wait for all of them to complete using Promise.all : async function bar(i) { console.log('started', i); await delay(1000); console.log('finished', i); } async function foo() { await Promise.all([bar(1), bar(2)]); } // This works too: async function my_all(promises) { for (let p of promises) await p; } async function foo() { await my_all([bar(1), bar(2), bar(3)]); } I tried to rewrite the latter in python: import asyncio async def bar(i): print('started', i) await asyncio.sleep(1) print('finished', i) async def aio_all(seq):

多线程笔记(一)Future

删除回忆录丶 提交于 2019-11-27 19:14:20
1. Future 和 FutureTask 获取返回结果 都是调用 ExecutorService 的 submit 方法,不同的是一个 submit Callable,返回值用 Future 接收;另一个 submit FutureTask,也就是 Runnable,不需要接收返回值 1 // 使用 Future,MyTask 是一个 Callable 的实现类 2 ExecutorService executorService = Executors.newCachedThreadPool(); 3 MyTask task = new MyTask(); 4 5 Future<Integer> result = executorService.submit(task); 6 executorService.shutdown(); FutureTask: 1 // 使用 FutureTask 2 ExecutorService executorService = Executors.newCachedThreadPool(); 3 MyTask myTask = new MyTask(); 4 FutureTask<Integer> futureTask = new FutureTask<>(myTask); 5 executorService.submit

How to compose Observables to avoid the given nested and dependent callbacks?

牧云@^-^@ 提交于 2019-11-27 18:12:01
In this blog , he gives this (copy/pasted the following code) example for the callback hell. However, there is no mention of how the issue can be eliminated by using Reactive Extensions. So here F3 depends upon F1 completion and F4 and F5 depend upon F2 completion. Wondering what would be the functional equivalent in Rx. How to represent in Rx that F1, F2, F3, F4 and F5 should all be pulled asynchronously? NOTE: I am currently trying to wrap my head around Rx so I didn't try solving this example before asking this question. import java.util.concurrent.CountDownLatch; import java.util

What is the best way to handle an ExecutionException?

↘锁芯ラ 提交于 2019-11-27 17:42:40
I have a method that performs some task with a timeout. I use the ExecutorServer.submit() to get a Future object, and then I call future.get() with a timeout. This is working fine, but my question is the best way to handle checked exceptions that can be thrown by my task. The following code works, and preserves the checked exceptions, but it seems extremely clumsy and prone to break if the list of checked exceptions in the method signature changes. Any suggestions on how to fix this? I need to target Java 5, but I'd also be curious to know if there are good solutions in newer versions of Java.

Why should I use std::async?

大憨熊 提交于 2019-11-27 17:16:57
I'm trying to explore all the options of the new C++11 standard in depth, while using std::async and reading its definition, I noticed 2 things, at least under linux with gcc 4.8.1 : it's called async , but it got a really "sequential behaviour", basically in the row where you call the future associated with your async function foo , the program blocks until the execution of foo it's completed. it depends on the exact same external library as others, and better, non-blocking solutions, which means pthread , if you want to use std::async you need pthread. at this point it's natural for me

Futures do not run before program termination

不羁的心 提交于 2019-11-27 15:09:47
I was trying to reproduce the example on new Scala 2.10 futures feature . The code I've used is: import scala.concurrent.Future import scala.concurrent.future object Test { def main(args: Array[String]) { println("Test print before future") val s = "Hello" val f = future {s + " future!"} f onSuccess {case v => println(v)} println("Test print after future") } } Instead of printing: Test print before future Hello future! Test print after future It simply prints: Test print before future Test print after future Any idea of why I have this behaviour? My version of scala compiler is 2.10.0-20120507