Strand class & continuation example

Remi Forax forax at univ-mlv.fr
Wed Jan 30 23:37:08 UTC 2019


Hi !

----- Mail original -----
> De: "Arkadiusz Gasiński" <jigga at jigga.pl>
> À: "loom-dev" <loom-dev at openjdk.java.net>
> 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:
  https://github.com/forax/loom-fiber/tree/master/src/main/java/fr.umlv.loom/fr/umlv/loom/example


> 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
>   }
> });
> cont.run();
> 
> while (!cont.isDone()) {
>   // try yield the continuation
>   Continuation.yield(scope);
>   // sleep some
>   try {
>      TimeUnit.SECONDS.sleep(3);
>   } catch (InterruptedException e) {
>      // ignore
>   } finally {
>      cont.run();
>   }
> }

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 cont.run() 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

cheers,
Rémi


More information about the loom-dev mailing list