【发布时间】:2010-11-30 04:13:51
【问题描述】:
Java 中有没有很好的延续实现?
如果是这样,开销是多少? JVM 在设计时并没有考虑到这些事情,对吧?那么这样的做法是不是逆天了?
【问题讨论】:
标签: java continuations
Java 中有没有很好的延续实现?
如果是这样,开销是多少? JVM 在设计时并没有考虑到这些事情,对吧?那么这样的做法是不是逆天了?
【问题讨论】:
标签: java continuations
Jetty 支持continuation。在DZone 有进一步的讨论和一些示例。
我无法就效率或其他方面提供建议,只能说 Mortbay 团队似乎总是意识到这些问题。很可能会在 Jetty 网站的某个地方讨论实现权衡。
【讨论】:
如果我理解正确的话,我想最明显的问题是在闭包实例处于活动状态的情况下展开堆栈。我想一种具有词法范围的语言理论上可以计算出一个子框架可以创建一个闭包实例,识别那些被引用的中间框架,然后它可以分配这些框架,而不是仅仅将它们推入堆栈。
就此而言,编译器可以分配引用非全局绑定对象的闭包的所有帧或所有父帧。
我认为 JVM 对闭包的限制并不比真正的机器多,只是它们与一般堆栈范式作斗争,因此它们通常会受到打击。
【讨论】:
请参阅 Apache Javaflow http://commons.apache.org/sandbox/javaflow/
它是唯一一个正在积极开发的 Java 延续包。另一个,RIFE,我不确定它在哪个州。
【讨论】:
如果您不介意隐式延续,Kilim 是一个不错的选择。它通过处理带注释的方法并为您生成字节码中的延续来工作。显然,由于它是一个框架,它的功能要多得多,但是如果您想要线程安全延续的(出色)性能,那么值得一看。
【讨论】:
玩!框架版本 1.2.x 还集成了 support for continuations 与异步 http 好东西。
注意Play 1.2.x continuations only work with the inbuilt Netty server。
【讨论】:
Scala 也可以在 JVM 上运行。所以它可能是相关的。
What are Scala continuations and why use them?
此外,Scala 也有一些类似的 async/await 特性:
【讨论】:
Java 流 http://commons.apache.org/sandbox/javaflow/ 播放框架使用Javaflow http://blog.heroku.com/archives/2011/8/29/play/
RIFE http://www.artima.com/lejava/articles/continuations.html WebWork 使用。
JauVM http://jauvm.blogspot.com/2005/07/so-what-does-it-do.html JVM中的JVM,实现尾调用/延续
斯卡拉 2.8 http://www.scala-lang.org/node/2096
茧 http://cocoon.apache.org/2.1/userdocs/flow/continuations.html http://wiki.apache.org/cocoon/RhinoWithContinuations
码头 http://docs.codehaus.org/display/JETTY/Continuations 重试请求。
协程 http://code.google.com/p/coroutines
jconts https://github.com/idubrov/jconts
jyield http://code.google.com/p/jyield
基林 http://www.malhar.net/sriram/kilim/thread_of_ones_own.pdf
【讨论】:
【讨论】:
从 Java 8 开始,现在有一个 CompletableFuture<T> 类,它支持延续和更多的函数式/反应式编程方法。
考虑以下示例,其中一个类提供了一个downloadAndResize 方法:
public CompletableFuture<Image> downloadAndResize(String imageUrl, int width, int height) {
return CompletableFuture
.supplyAsync(() -> downloadImage(imageUrl))
.thenApplyAsync(x -> resizeImage(x, width, height));
}
private Image downloadImage(String url){
// TODO Download the image from the given url...
}
private Image resizeImage(Image source, int width, int height){
// TODO Resize the image to w / h
}
上述方法的用法如下:
CompletableFuture<Image> imagePromise = downloadAndResize("http://some/url", 300, 200);
imagePromise.thenAccept(image -> {
// Gets executed when the image task has successfully completed
// do something with the image
});
【讨论】:
CompletableFuture 只是很好地包装和使用它们的一种可能方式。
最近又出现了一个强大的竞争对手。
Quasar 使用来自 Matthias Mann 的 java 实现 continuations 中的 forked 来提供更高级别的功能,例如 lightweight threads、Erlang-like actors 和 Go-like coroutines 和 channels。
Quasar Blog中有很多基准测试和详细介绍。
还有一个名为 Comsat 的即用型集成,旨在帮助轻松构建基于后台延续机制的高性能 Web 服务。
Quasar 还提供了一个不错的 Kotlin API,该 API 在最近的 JetBrains 网络研讨会上得到了展示 Quasar: Efficient and Elegant Fibers, Channels and Actors.
提到的所有内容都是开源的并且可以免费使用。
另见http://blog.paralleluniverse.co/2015/08/07/scoped-continuations/
更新
Quasar 的经验后来被用作 Loom Project 的基础,aims 在 Java 11 之后的某个时间将继续支持直接引入 JVM。
它现在在active development 下,并且已经有一个有效的alpha prototype。
【讨论】:
同时考虑Kotlin Coroutines。
它是implemented,可能是性能更高的CPS transformations (still stackful),并且可以在后台使用任何异步执行器,例如 ForkJoinPool 或Quasar integration。
提防一些tooling 和reflection 的陷阱。
【讨论】: