Continuations in Java

前端 未结 11 1969
再見小時候
再見小時候 2020-12-05 00:20

Is there a good implementation of continuations in Java?

If so, what is the overhead like? The JVM wasn\'t designed with these sort of things in mind, right? So is t

相关标签:
11条回答
  • 2020-12-05 01:03

    See 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.

    0 讨论(0)
  • 2020-12-05 01:05

    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.

    For that matter, a compiler could malloc all frames or all parent frames of a closure referencing a non-globally-bound object.

    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.

    0 讨论(0)
  • 2020-12-05 01:05

    Another strong competitior has appeared recently.

    Quasar uses forked from Matthias Mann's implementation of java continuations to provide higher level features like lightweight threads, Erlang-like actors and Go-like coroutines and channels.

    There are many benchmarks and detailed introductions in the Quasar Blog.

    There is also ready-to-use integration named Comsat aimed to help easily building performant web services based on continuations machinery under the hood.

    Quasar also provides a nice Kotlin API that was featured on recent JetBrains webinar 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/


    Update

    Quasar's experience was later used as foundation for the Loom Project which aims to bring continuations support directly into JVM sometime past Java 11.

    It's under active development now and already has a working alpha prototype.

    0 讨论(0)
  • 2020-12-05 01:08

    Since Java 8, there is now a CompletableFuture<T> class which supports continuations and more functional / reactive programming approaches.

    Consider the following example, where a Class offers a downloadAndResize method:

    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
    
    });
    
    0 讨论(0)
  • 2020-12-05 01:11

    Javaflow http://commons.apache.org/sandbox/javaflow/ Play framework use Javaflow http://blog.heroku.com/archives/2011/8/29/play/

    RIFE http://www.artima.com/lejava/articles/continuations.html WebWork use.

    JauVM http://jauvm.blogspot.com/2005/07/so-what-does-it-do.html JVM in JVM, implements tail call / continuation

    Scala 2.8 http://www.scala-lang.org/node/2096

    Cocoon http://cocoon.apache.org/2.1/userdocs/flow/continuations.html http://wiki.apache.org/cocoon/RhinoWithContinuations

    Jetty http://docs.codehaus.org/display/JETTY/Continuations retry request.

    coroutines http://code.google.com/p/coroutines

    jconts https://github.com/idubrov/jconts

    jyield http://code.google.com/p/jyield

    Kilim http://www.malhar.net/sriram/kilim/thread_of_ones_own.pdf

    ATCT http://portal.acm.org/ft_gateway.cfm?id=949362

    0 讨论(0)
提交回复
热议问题