Strand class & continuation example

Remi Forax forax at
Wed Jan 30 23:37:08 UTC 2019

Hi !

----- Mail original -----
> De: "Arkadiusz Gasiński" <jigga at>
> À: "loom-dev" <loom-dev at>
> Envoyé: Mercredi 30 Janvier 2019 19:11:50
> Objet: Strand class & continuation example

> Hi,
> I assume that you're all busy with the loom project, but if anyone here
> finds some time to briefly explain the rationale behind removal of the
> Strand class, I'll be really grateful.

Ron or Alan may correct me but
the idea of a Strand was that at some point you want consider a Fiber or a Thread as a common strand, but given that their API are different, you can not do much on a strand apart asking if it's a fiber or a thread. That said, you may have a use case were a strand make sense, in that case do not hesitate to share it on this list.

> I tried to find some explanation in the mailing list archive, but to no
> avail.
> Also, if you could provide some very basic example of Continuation usage
> without involving Fibers 

You have some simple examples using continuations here:

> as I'm struggling with the below code (perhaps I
> got the whole idea wrong):
> var scope = new ContinuationScope("main");
> Continuation cont = new Continuation(scope, () -> {
>   while (true) {
>      // do very important stuff
>   }
> });
> while (!cont.isDone()) {
>   // try yield the continuation
>   Continuation.yield(scope);
>   // sleep some
>   try {
>      TimeUnit.SECONDS.sleep(3);
>   } catch (InterruptedException e) {
>      // ignore
>   } finally {
>   }
> }

You yield from within the continuation, i.e. Continuation.yield() as to be called inside the code of the runnable.

Continuations are a form of cooperative concurrency, there is no scheduler that will stop you from executing the code of the continuation, you have to explicitly yield() to go back at the point you have called run() and if you call run again, it will restart the continuation just after the last call to yield().

> As you probably guess by now, I can't get past the first call as
> it block main. If I run the continuation for the first time in a separate
> thread, then Continuation.yield(scope) throws IllegalArgumentException("Not
> in scope " + scope).
> So basically what I'd like to achieve is to run some continuation, yield it
> after a while and then resume it from where it was before yielding, if of
> course, this is possible.
> Thanks,
> Arek


More information about the loom-dev mailing list