Virtual Thread Scheduler - why ForkJoinPool?
Florian Schmaus
flow at cs.fau.de
Tue Oct 4 15:31:48 UTC 2022
On 04.10.22 16:15, Jonas Schlecht wrote:
> Hi everybody,
Hi Jonas,
> I am currently writing my thesis which, for some part, also covers
> Virtual Threads. So far, I get how everything works but I don’t
> understand how the work-stealing nature of the ForkJoinPool is used by
> Virtual Threads.
> I know the benefits of a work-stealing scheduler and how the
> ForkJoinPool uses ForkJoinTasks. But how do Virtual Threads „fork“ tasks
> which can be stolen by other threads?
Basically, VirtualThread.submitRunContination() [1] submits the virtual
thread's continuation into the fork/join pool. This typically happens
when a virtual thread is unparked, e.g., when it became runnable again
after it was blocked on a synchronization primitive.
There are potentially more cases where the runtime systems puts a
virtual thread (or a continuation of thereof) into the fork/join pool. I
am sure the Loom experts can fill those in.
> As far as I
> understand it, the same scheduling result could be achieved by using any
> other thread pool with the amount of available CPU cores as the number
> of threads.
That appears to be re-inventing the wheel. :)
> After all, the ForkJoinPool needs ForkJoinTasks to use the
> work-stealing logic. Or am I mistaken here?
You are right: A ForkJoinPool would like to be feed with ForkJoinTasks.
But a ForkJoinTask can easily be created from a Runnable via
ForkJoinTask.adapt(Runnable).
> Could you maybe point me to some ressources that explain why you decided
> to use the ForkJoinPool and how it is used? I couldn’t find any online.
Work-stealing scheduling is a perfect fit for concurrency platforms like
Loom, and ForkJoinPool already implemented work-stealing. But I was not
the one who decided this, so please do not consider this an
authoritative answer.
I hope that I could help and point you into the right direction.
Best Wishes
Florian
1:
https://github.com/openjdk/loom/blob/3afb61b8fcc150595d6e5d2f7981b99dacf3b5be/src/java.base/share/classes/java/lang/VirtualThread.java#L233
More information about the loom-dev
mailing list