Coroutine questions
Mike Hearn
michael.hearn at oracle.com
Wed Mar 19 09:48:28 UTC 2025
If you’d like to experiment with these features, including serialization of the stack frames, the Espresso JVM has an experimental API that enables them along with sample code showing how to build generators and Kryo-serialized subprogram snapshots.
https://www.graalvm.org/reference-manual/espresso/continuations/
https://www.graalvm.org/reference-manual/espresso/continuations/serialization/
https://www.graalvm.org/reference-manual/espresso/continuations/generators/
It may be enough for you to explore whatever it is you’re exploring.
There are caveats!
• The API will change dramatically.
• Espresso is a a very new JVM with different features, performance and maturity levels to HotSpot.
The planned API changes will bring it much closer to Loom philosophically, such that it might one day be the basis for a JEP. The current API is a conventional one exposes a Continuation class which lets you mount and unmount stack frames onto the current thread. Loom chooses not to expose that capability for technical reasons. We’ll go in the same direction, most likely by allowing the whole stack of a virtual thread to be copied into a LinkedList<StackFrame> or SerializableThread type structure, and also reconstituted from one. It means no net-new concepts in Java, resolves some edge cases around Thread.currentThread() stability assumptions, and because context switching virtual threads is so fast features like yielding values from a generator can be done using ordinary inter-thread constructs as you suggest.
> On 18 Mar 2025, at 17:39, common1945 at tutanota.com wrote:
>
> I am doing some experiments that will make use of loom's Continuation with jdk22+. I know that I can achieve java Continuation effect with following code. However, I have a few questions:
>
> • Is it possible to pass in arguments like Lua's coroutine[1][2]?
> • Is it possible to yield/ return some values like Lua's coroutine[1][2]?
> • Does the way to communicate between the Continuation block i.e. Runnable and outside Runnable merely be able to achieved by embedding e.g. BlockingQueue inside the Runnable code block?
> • Is it possible to capture Continuation's snapshot for later use?
> Many thanks
>
> import jdk.internal.vm.ContinuationScope;
> import jdk.internal.vm.Continuation;
>
> public class C {
>
> public static void main(String[] args) {
> var scope = new ContinuationScope("hello");
> var yield = Continuation::yield;
> var continuation = new Continuation(scope, () -> {
> System.out.println("C1");
> Continuation.yield(scope);
> System.out.println("C2");
> Continuation.yield(scope);
> System.out.println("C3");
> });
> System.out.println("start");
> continuation.run();
> System.out.println("came back");
> continuation.run();
> System.out.println("back again");
> continuation.run();
> System.out.println("back again again");
> }
> }
>
>
> [1]. https://stackoverflow.com/questions/38069751/confusion-about-lua-corountines-resume-and-yield-function/38075905#38075905
>
> [2]. https://www.lua.org/pil/9.1.html
More information about the loom-dev
mailing list