Thanks for the context Alan and Viktor. I guess since Virtual Threads kick off tasks so fast (due to their throughput), I never had a chance to observe this behaviour.
But I think I get it -- once the scope closes, all future calls to fork() fail, so onFork() simply doesn't get called. That is a very important detail though. Hope you don't mind, but let me change subject to make sure I understand this behaviour. For example, let's say I use the anySuccessfulOrThrow Joiner. Then, theoretically speaking, if I have enough tasks such that by the time the scope closes, I am still feeding tasks into fork(), then my scope will fail? Am I understanding that correctly? On Sun, Aug 24, 2025, 7:21 AM Viktor Klang <viktor.kl...@oracle.com> wrote: > Not sure this applies to your specific situation, David, but for the other > readers of this conversation I also want to raise the point that > implementing a Joiner must be done in a thread-safe manner. > > Cheers, > √ > > > *Viktor Klang* > Software Architect, Java Platform Group > Oracle > ------------------------------ > *From:* loom-dev <loom-dev-r...@openjdk.org> on behalf of Alan Bateman < > alan.bate...@oracle.com> > *Sent:* Sunday, 24 August 2025 08:19 > *To:* David Alayachew <davidalayac...@gmail.com> > *Cc:* loom-dev <loom-dev@openjdk.org> > *Subject:* Re: When should I use onFork? > > > > On 23/08/2025 20:27, David Alayachew wrote: > > Woah, very good to know. And thus, even when the scope is closed, > > onFork will still be called is what you are saying? > > > > Thanks for this. Very useful. > > > No, fork is specified to throw IllegalStateException if closed. My > comment is pointing out that if you cancel the scope then the result > from any subtasks that are still executing does not contribute to the > outcome. Once you cancel then onComplete for these "still running (and > interrupted)" subtasks won't be called. To put it another way, if the > outcome from join is something derived from all subtasks that were > forked then you'll need to implement onFork. It may be more common that > the outcome is derived from the result/exception from a subset of the > subtasks, in which case it will be the onComplete method that you > implement. > > -Alan >