future

An Akka/Scala promise that requires two actors to complete

江枫思渺然 提交于 2019-12-10 10:38:45
问题 I am building a stock market application using Scala and Akka. The market matches buyers and sellers and then sends a Promise[Transaction] to both the buyer and the seller that needs to be completed (at some point) in order for the transaction to be processed. Issue is that the promise could complete with failure because either buyer has insufficient funds, seller has insufficient shares. How can I create a Scala promise that requires the coordination of two actors to complete? 回答1: You are

Queryover dynamic fetch with joins

点点圈 提交于 2019-12-10 10:37:28
问题 iam trying a new query with nhibernate and find a new problem :( take this as model: public class D { int id; } public class C { int id; } public class B { int id; ICollection<C> Cs; ICollection<D> Ds; } public class A { int id; ICollection<B> Bs; } i want A object that have a particular B object and dinamically eager fetch Cs or Ds collection of selected B: public virtual A Read(int idB, params Expression<Func<Attivita, object>>[] eagerFields) i start with IEnumerable<A> query = _session

Future cancel method documentation

柔情痞子 提交于 2019-12-10 10:31:30
问题 According to http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html isDone returns true if cancel(boolean mayInterruptIfRunning) was called. After this method returns, subsequent calls to isDone() will always return true. However, it is possible that task is running and mayInterruptIfRunning is set to false . So, what should return isDone() right after that call? true because of cancel (which is wrong)? Also, it's not clear whether cancel(boolean) method returns false . P.

Akka2使用探索3(Duration 和 Deadline)

大城市里の小女人 提交于 2019-12-10 07:01:38
akka提供了两个关于时长的数据类型:Duration 和 Deadline,比如5秒钟这种含义。 Duration.Inf表示无限, Duration.MinusInf表示负无限 Deadline , 表示一个绝对的时间点,意义是最终期限,并且支持通过计算当前时间到deadline之间的差距来生成Duration 下面是使用方法: import akka.util.Duration import java.util.concurrent.TimeUnit import akka.util.FiniteDuration import akka.util.Deadline import akka.dispatch.Await import akka.dispatch.Futures import java.util.concurrent.Callable import akka.dispatch.Future import akka.actor.ActorSystem class DurationTest extends GroovyTestCase { def testDurationUse() { println new FiniteDuration(5, TimeUnit.SECONDS) println Duration.create(5d, TimeUnit.SECONDS

Does a wait on Scala Future block thread?

巧了我就是萌 提交于 2019-12-10 03:39:34
问题 When I wait for result of Scala Future, does it behave more like receive , or like react , i.e. does it block a thread, or schedules a continuation after result if available? 回答1: Yes, in stdlib it blocks the thread, and synchronously waits for results. If you want to apply continuation-passing style to futures, you'd have to use Akka or Scalaz that allow adding hooks on futures completion straight from the box. Akka: val f1 = Future { Thread.sleep(1000); "Hello" + "World" } val f2 = f1 map {

Unit test succeeds in debug mode but fails when running it normally

自古美人都是妖i 提交于 2019-12-10 01:56:46
问题 Why does my unit test succeed in debug mode but fail when running it normally? public class ExecutorServiceTest extends MockitoTestCase{ private int numThreads; private ExecutorService pool; private volatile boolean interruptedBitSet; @Override public void setUp() { numThreads = 5; pool = Executors.newFixedThreadPool(numThreads); } class TaskChecksForInterruptedBit implements Callable<String> { @Override public String call() throws Exception { interruptedBitSet = false; while (!Thread

Should I return CompletableFuture or Future when defining API?

送分小仙女□ 提交于 2019-12-10 01:45:56
问题 In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture instead of returning Future ? Considering it is ugly converting Future to CompletableFuture and the fact that CompletableFuture will give the caller more flexibility of using functional style directly, what could be a good reason for an API to just return Future ? 回答1: My 2 cts: by returning a Future, you keep your options open and can return a Future, or a CompletableFuture - it makes no

Android volley Timeout Exception when using RequestFuture.get()

被刻印的时光 ゝ 提交于 2019-12-09 15:05:13
问题 In my Fragment, i am trying to use TMDB's open movie DB to get details about "Now Playing" Movies. If i use RequestFuture.get(time, TimeUnit) method to execute this volley request i always get a timeout error. If i manually test the same Url in Safari, i get the results instantaneously. What I know: 1.) It is not any JSON Parsing error.(the program doesnt even progress to the parsing steps) 2.) No internet issues with AVD. (Reason explained later). 3.) Not an issue with my volley singleton

BlackHole开发日记-多线程NIO和超时机制

烈酒焚心 提交于 2019-12-09 14:32:07
今天尝试使用Selector改造转发逻辑,结果可耻的失败了!因为 Selector不是线程安全的 ,试图多个线程进行register会导致严重的问题,这也是为什么基于事件的IO模型都不怎么支持多线程的原因,太难做了。 后来尝试用Java的Future机制来实现超时。我们知道,Java的FutureTask需要一个执行的线程池。如果每次都new出来当然没有问题,但是经测试性能开销较大(qps被降到了4000)。后来尝试复用同一大线程池,发现请求多了之后,总是会超时! 然后jstack查看发现,很多线程仍然卡在了Callable.call方法,就是我们常说的异常把线程抛死了!原来TimeoutException也会导致线程抛出异常但是无法回收。解决方法:使用future.cancel()。 这也说明,线程抛出异常死掉时,jstack查看到的是它抛出异常时的执行栈。 加入超时机制后,程序算是稳定了,这周末弄个1.0版吧,以后开始写ReleaseNotes。 今天看了一下代码,作为一个开源项目,似乎风格不是那么优雅,很多长函数,注释也不完整。顶多对架构和原理感兴趣,代码的OOP神马的,这方面完全提不起兴趣来啊。 在自己的MBP上也编译了一个queryperf,测试性能达到55000qps,而拦截模式只有35000,果然还是缓存byte[]更给力啊。因此把拦截模式也加入了cache。 来源:

Why is `Future::poll` not called repeatedly after returning `NotReady`?

心已入冬 提交于 2019-12-09 13:32:11
问题 Consider the following code extern crate futures; use std::sync::{atomic, Arc}; use futures::*; struct F(Arc<atomic::AtomicBool>); impl Future for F { type Item = (); type Error = (); fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> { println!("Check if flag is set"); if self.0.load(atomic::Ordering::Relaxed) { Ok(Async::Ready(())) } else { Ok(Async::NotReady) } } } fn main() { let flag = Arc::new(atomic::AtomicBool::new(false)); let future = F(flag.clone()); ::std::thread::spawn