Java 中的延续
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1456083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Continuations in Java
提问by Mike
Is there a good implementation of continuations in Java?
Java 中是否有一个很好的延续实现?
If so, what is the overhead like? The JVM wasn't designed with these sort of things in mind, right? So is this kind of going against the grain?
如果是这样,开销是多少?JVM 的设计并没有考虑到这些事情,对吧?那么,这种做法是不是背道而驰呢?
回答by gawi
See Apache Javaflow http://commons.apache.org/sandbox/javaflow/
参见 Apache Javaflow http://commons.apache.org/sandbox/javaflow/
It's the only continuation package for java that's actively under development. The other one, RIFE, I'm not sure which state it's in.
它是唯一一个正在积极开发的 java 延续包。另一个,RIFE,我不确定它在哪个州。
回答by Jerry Chan
Javaflow http://commons.apache.org/sandbox/javaflow/Play framework use Javaflow http://blog.heroku.com/archives/2011/8/29/play/
Javaflow http://commons.apache.org/sandbox/javaflow/Play框架使用Javaflow http://blog.heroku.com/archives/2011/8/29/play/
RIFE http://www.artima.com/lejava/articles/continuations.htmlWebWork use.
RIFE http://www.artima.com/lejava/articles/continuations.htmlWebWork 使用。
JauVM http://jauvm.blogspot.com/2005/07/so-what-does-it-do.htmlJVM in JVM, implements tail call / continuation
JauVM http://jauvm.blogspot.com/2005/07/so-what-does-it-do.htmlJVM中的JVM,实现尾调用/延续
Scala 2.8 http://www.scala-lang.org/node/2096
Scala 2.8 http://www.scala-lang.org/node/2096
Cocoon http://cocoon.apache.org/2.1/userdocs/flow/continuations.htmlhttp://wiki.apache.org/cocoon/RhinoWithContinuations
茧 http://cocoon.apache.org/2.1/userdocs/flow/continuations.html http://wiki.apache.org/cocoon/RhinoWithContinuations
Jetty http://docs.codehaus.org/display/JETTY/Continuationsretry request.
Jetty http://docs.codehaus.org/display/JETTY/Continuations重试请求。
coroutines http://code.google.com/p/coroutines
协程 http://code.google.com/p/coroutines
jconts https://github.com/idubrov/jconts
jconts https://github.com/idubrov/jconts
jyield http://code.google.com/p/jyield
jyield http://code.google.com/p/jyield
Kilim http://www.malhar.net/sriram/kilim/thread_of_ones_own.pdf
Kilim http://www.malhar.net/sriram/kilim/thread_of_ones_own.pdf
回答by Brian Agnew
Jettyhas continuationsupport. There is further discussion and some samples at DZone.
Jetty有持续支持。在DZone有进一步的讨论和一些示例。
I can't advise on the efficiencies or otherwise, other than to say that the Mortbay team always appear concious of such issues. There will most likely be a discussion of implementation trade-offs somewhere on the Jetty site.
我不能就效率或其他方面提出建议,只能说 Mortbay 团队似乎总是意识到这些问题。Jetty 站点上的某个地方很可能会讨论实现权衡。
回答by DigitalRoss
If I understand this correctly, I suppose the obvious problem involves unwinding the stack with closure instances active. I suppose a language with lexical scope could in theory figure out that a child frame may create a closure instance, identify those intermediate frames that are referenced, and then it could malloc those frames instead of just pushing them on the stack.
如果我理解正确,我认为明显的问题涉及在关闭实例处于活动状态的情况下展开堆栈。我认为具有词法作用域的语言理论上可以确定子框架可以创建一个闭包实例,识别那些被引用的中间框架,然后它可以对这些框架进行 malloc,而不是仅仅将它们推送到堆栈上。
For that matter, a compiler could malloc all frames or all parent frames of a closure referencing a non-globally-bound object.
就此而言,编译器可以 malloc 引用非全局绑定对象的闭包的所有帧或所有父帧。
Summary
概括
I don't think the JVM restricts closures any more than a real machine, it's just that they fight the general stack paradigm and so they usually get punted.
我认为 JVM 对闭包的限制并不比真正的机器多,只是它们与一般的堆栈范式作斗争,因此它们通常会受到打击。
回答by ngreen
If you don't mind implicit continuations, Kilimis a great option. It works by processing annotated methods and generating the continuations in bytecode for you. Obviously it does a lot more since it's a framework, but if you want the (excellent) performance of thread-safe continuations, it's worth a look.
如果您不介意隐式延续,Kilim是一个不错的选择。它通过处理带注释的方法并为您生成字节码中的延续来工作。显然它做的更多,因为它是一个框架,但如果你想要线程安全延续的(优秀)性能,那么值得一看。
回答by IsNull
Since Java 8, there is now a CompletableFuture<T>class which supports continuations and more functional / reactive programming approaches.
从 Java 8 开始,现在有一个CompletableFuture<T>支持延续和更多函数式/反应式编程方法的类。
Consider the following example, where a Class offers a downloadAndResizemethod:
考虑以下示例,其中 Class 提供了一个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
}
Usage of the above method could look like:
上述方法的用法可能如下所示:
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
});
回答by Vadzim
Another strong competitior has appeared recently.
最近又出现了一个强大的竞争对手。
Quasaruses forkedfrom Matthias Mann's implementation of java continuationsto provide higher level features like lightweight threads, Erlang-like actorsand Go-like coroutines and channels.
Quasar使用来自 Matthias Mann 的 Java延续实现的fork来提供更高级别的功能,例如轻量级线程、类似 Erlang 的actor和类似 Go 的协程和通道。
There are many benchmarks and detailed introductions in the Quasar Blog.
Quasar博客中有很多benchmarks和详细介绍。
There is also ready-to-use integration named Comsataimed to help easily building performant web services based on continuations machinery under the hood.
还有一个名为Comsat 的即用型集成,旨在帮助轻松构建基于引擎盖下的延续机制的高性能 Web 服务。
Quasar also provides a nice Kotlin API that was featured on recent JetBrains webinar Quasar: Efficient and Elegant Fibers, Channels and Actors.
Quasar 还提供了一个很好的 Kotlin API,它在最近的 JetBrains 网络研讨会 Quasar: Efficient and Elegant Fibers, Channels and Actors 中有特色。
Everything mentioned is open-source and free to use.
提到的一切都是开源的,可以免费使用。
See also http://blog.paralleluniverse.co/2015/08/07/scoped-continuations/
另见http://blog.paralleluniverse.co/2015/08/07/scoped-continuations/
Update
更新
Quasar's experience was later used as foundation for the Loom Projectwhich aimsto bring continuations support directly into JVM sometime past Java 11.
类星体的经验后来被用作基础织机项目,其目标带来的延续支持直接插入JVM过去的某个时候Java的11。
It's under active developmentnow and already has a working alpha prototype.
回答by Vadzim
Consider also Kotlin Coroutines.
还要考虑Kotlin Coroutines。
It's implementedvia potentially more performant CPS transformations(still stackful) and can use any async executor under the hood like ForkJoinPool or Quasar integration.
它实现通过潜在的更高性能的CPS转换(仍然stackful),并可以像ForkJoinPool或引擎盖下使用任何异步执行类星体的整合。
Comes with handy guidedlibrary.
Beware of some toolingand reflectionpitfalls.
回答by Vadzim
Play! framework version 1.2.x also has support for continuationsintegrated with async http goodies.
玩!框架版本 1.2.x 还支持与异步 http 好东西集成的延续。
Note that Play 1.2.x continuations only work with the inbuilt Netty server.
请注意,Play 1.2.x 延续仅适用于内置 Netty 服务器。
回答by Vadzim
Scalaalso runs on JVM. So it might be relevant.
Scala也运行在 JVM 上。所以它可能是相关的。
What are Scala continuations and why use them?
In addition Scala has somewhat similar async/await feature:
此外 Scala 有一些类似的 async/await 特性:

