On Mon, 30 Nov 2026 10:30:24 GMT, Alan Bateman <[email protected]> wrote:
> The changes here improve fairness of virtual thread scheduling in two cases. > > 1. Thread.yield. The current implementation pushes the virtual thread task to > an external queue when the local queue is empty, and to the local queue when > non-empty. This has worked reasonably well but the externalSubmit comes with > queue-selection bias. The implementation is changed to use "randomSubmit" for > the empty local queue case, a significant improvement to fairness when > Thread.yield is used a lot. A future PR may expose ForkJoinPool.randomSubmit > as a public API. > > 2. LockSupport.unpark when a virtual thread unparks a lot of other virtual > threads. This includes I/O when many file descriptors for many virtual > threads may be polled in batch. The current implementation pushes each > virtual thread task to a local queue. This can lead to scenarios where all > scheduler threads are executing tasks taken from their local queue and where > it is rare to steal from external queues. The implementation is changed to > choose between the local queue and an external queue using > ForkJoinTask.getSurplusQueuedTaskCount (suggested by Doug Lea). This provides > a cheap estimate of spare work in the local queue, allowing for potential > stealers. As part of this change, ForkJoinTask.getSurplusQueuedTaskCount is > changed to use the carrier thread, similar to poolSubmit. > > Tests: For Thread.yield, a test is added to print a fairness index. It > samples virtual threads that alternate between a fixed amount of work and > Thread.yield and prints an index based on the Raj Jain fairness measure [1]. > The index range is 1/nTasks to 1.0, where 1.0 means all tasks made equal > progress. Baseline is ~0.7 changing to ~0.99 when using randomSubmit. The > test only prints the index, it can't assert a scheduling guarantee as it > would not be a reliable test. For unpark change, a test is added to ensure > that tasks are pushed to both the carrier's local queue and external queues. > > [1] https://en.wikipedia.org/wiki/Fairness_measure > > --------- > - [x] I confirm that I make this contribution in accordance with the [OpenJDK > Interim AI Policy](https://openjdk.org/legal/ai). src/java.base/share/classes/java/lang/VirtualThread.java line 404: > 402: if (currentThread() instanceof CarrierThread ct > 403: && ct.getQueuedTaskCount() == 0 > 404: && (pool = ct.getPool()) == scheduler) { Might be "better" to check the pool == scheduler *prior* to checking the queued task count (as the queued task count won't matter if it's not the right pool). src/java.base/share/classes/java/lang/VirtualThread.java line 613: > 611: // random submit if there are no tasks in the local task > queue > 612: if (currentThread() instanceof CarrierThread ct && > ct.getQueuedTaskCount() == 0) { > 613: randomSubmitRunContinuation(); 👍 src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java line 2990: > 2988: static int getSurplusQueuedTaskCount() { > 2989: ForkJoinPool pool; WorkQueue q; > 2990: if (((JLA.currentCarrierThread()) instanceof > ForkJoinWorkerThread wt) && Suggestion: if ((JLA.currentCarrierThread() instanceof ForkJoinWorkerThread wt) && test/jdk/java/lang/Thread/virtual/UnparkQueuing.java line 72: > 70: }); > 71: ready.await(); > 72: await(thread, Thread.State.WAITING); Do we need both the count down latch AND waiting for Thread.State.WAITING? (Can the wait occur prior to LockSupport.park()?) ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/34118#discussion_r4771949923 PR Review Comment: https://git.openjdk.org/jdk/pull/34118#discussion_r4771963716 PR Review Comment: https://git.openjdk.org/jdk/pull/34118#discussion_r4771969032 PR Review Comment: https://git.openjdk.org/jdk/pull/34118#discussion_r4772002387
