在最近工作中,遇到使用Apache mina 来模拟多个客户端登录期货交易系统,进行买卖交易。但是,在Code的过程中,发现自己的操作全部都是异步的。我采用异步+定时轮询的方式。但是,感觉这种方式不是最优解。然后,想到Apache Mina本身也是异步操作。以前,所有代码全部都是同步操作,对于异步了解比较少。所有对java语言本身对异步操作进行再次学习。
在学习过程中,了解到Trane.io Future 是对Twitter Tuture 特点的java实现。
Trane.io Future is a high-performance implementation of the Future abstraction. The library was designed from scratch with a focus on reducing CPU usage and memory footprint, and having the Twitter Future as its main inspiration.
It allows the user to express complex asynchronous code in a composable and type-safe manner. It also supports more advanced features that are currently only available for Twitter's Future and are essential to developing non-trivial systems.
The Future abstraction
Future is an abstraction to deal with asynchronicity without having to use callbacks directly or blocking threads. The primary usage for Futures on the JVM is to perform IO operations, which are asynchronous by nature.
Although most IO APIs return synchronously, they do that by blocking the current Thread. For instance, the thread issues a request to a remote system and then waits until a response comes back. Considering that the JVM uses native threads, it is wasteful to block them since it leads to potential thread starvation and higher garbage collection pressure. It is hard to scale a JVM system vertically if the IO throughput is bounded by the number of threads.
Trane.io Future 是一个Future 抽象的高性能实现。 Trane.io Future 是降低CPU利用率,内存占用,以Twitter Future 作为灵感 作为设计的。
Trane.io Future 允许用户表达复杂异步代码和安全。 Trane.io Future 同时支持Twitter Future更加高级的特性.同时也是开发与众不同系统的必须东西。
Trane.io Future 抽象
Future是不用回调函数和阻塞当前线程来处理异步操作的抽象。例如: 一个线程向远程系统发送一个请求,同时,等到响应。 考虑到JVM使用native thread ,阻塞线程是一种浪费。因为阻塞线程将导致线程饥饿和GC压力。 如果IO吞吐量被线程的数量线程,这个时候很难拓展JVM系统。
//执行异步操作
ExecutorService executors = Executors.newCachedThreadPool();
FuturePool futurePool = FuturePool.apply(executors);
//异步操作
Future<Integer> future = futurePool.async(new Supplier<Integer>() {
public Integer get() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" Sleep(1000)");
return new Integer(999);
}
});
//异步操作通知
future.onSuccess(new Consumer<Integer>() {
public void accept(Integer t) {
System.out.println("result:" + t);
}
});
在Trane.io Future 中主要有Future ,Promise ,FuturePool 接口。
public abstract class Promise<T> implements Future<T> { }
Promise is a Future that provides methods to set its result. They are useful to interact with callback-based APIs like the ones that are typically provided by network libraries.
A promise can be created and returned synchronously to the caller, but its completion is deferred until the value is set, typically by a callback.
Promise 是一个可以设置结果的Future。 使用Promise 作为一个类似Callback-based API的结果。 这种结果尤其是在网络库中。
Promise可以被调用者创建,同时同步返回, 直到计算结果完成时,结果值才被延缓设置到promise中。
参考链接:
Trane.io Future
https://github.com/traneio/future
Java并行程序设计模式小结
http://www.cnblogs.com/panfeng412/p/java-program-tuning-reading-notes-of-concurrent-program-design-pattern.html
什么是Future模式
http://www.cnblogs.com/uptownBoy/articles/1772483.html