最近小沃正在研究vert.x框架,这套基于netty的框架,大量使用了异步IO,因此在并发性能上比类似tomcat这类框架要高得多,但是高性能也带来了一些麻烦,那就是大量使用了回调函数。由于大量使用了回调函数。当需要回调嵌套时,就会陷入回调地狱,之前使用nodejs时,有Promise,同时还有类似async与await等方式,但是在vert.x中,包括java8中都没有类似的方法。
没办法,小沃就只有自己写一个java版本的Promise类了。
考虑到实际使用很有可能是如下结构:new Promise() {
@Override
public void run(int n) {
System.out.println("hello1 n:" + n);
this.resovle(1);
}
}.then(new Promise() {
@Override
public void run(int n) {
System.out.println("hello2 n:" + n);
this.resovle(2);
}
}).then(new Promise() {
@Override
public void run(int n) {
System.out.println("hello3 n:" + n);
this.resovle(3);
}
}).then(new Promise