views:

371

answers:

4

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 this kind of going against the grain?

+3  A: 

Jetty has continuation support. There is further discussion and some samples at 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.

Brian Agnew
It seems the Jetty continuation implementation is tied up with its Servlet container, so I don't think this can help me.
Mike
Jetty continuations are not real continuations in the programming language sense. It's just a trick to re-trigger a request processing.
gawi
A: 

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.

DigitalRoss
A: 

You can't do continuations in Java.

Mike
A: 

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.

gawi