Virtual Thread Scheduler - why ForkJoinPool?

Alan Bateman Alan.Bateman at oracle.com
Wed Oct 5 09:05:26 UTC 2022


On 04/10/2022 15:15, Jonas Schlecht wrote:
>
> Hi everybody,
>
> 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? Do they even do that? 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. After all, the ForkJoinPool needs 
> ForkJoinTasks to use the work-stealing logic. Or am I mistaken here?
>
> 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.
>

For starters, think of ForkJoinPool as a "better thread pool". It has 
many advantages over a thread pool that uses a shared blocking queue for 
all tasks.

Another thing is a ForkJoinPool can be created in "async mode" which is 
local FIFO scheduling. This is good for applications doing message 
passing and also good for scheduling the tasks for virtual threads.

As others have pointed out, scheduling a virtual thread to execute 
causes a special task for the thread to be pushed to one of the FJP 
submission queues. A virtual thread T1 unparking virtual thread T2 will 
push the task for T2 to the submission queue of T1's carrier (worker 
thread). It may be that T2's task is executed by the that worker thread 
or it may be that some worker thread steals the task.

There are a few other features of ForkJoinPool that you might want to 
look into. One is that it parallelism can be dynamically changed when 
workers are blocked - you'll see this is used to smooth over cases where 
virtual threads temporarily pin their carrier during file I/O 
operations. Another recent addition to ForkJoinPool is the ability to 
submit a task without signalling, look for "lazySubmit". This is used to 
reduce steals in very specific cases such as when a thread is unparked 
while parking.

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20221005/ec6006c9/attachment-0001.htm>


More information about the loom-dev mailing list