Thread::yield in a VirtualThread

Alan Bateman Alan.Bateman at oracle.com
Tue Jan 3 10:18:00 UTC 2023


On 26/12/2022 22:59, KT Nabe wrote:
> In writing a simulation for my students, I've encountered some unexpected behavior when virtual threads invoke Thread.yield (understanding that it isn't really clear what guarantees if any are provided).
>
> In short, if enough* virtual threads are concurrently executing Thread.yield, then no other suspended (sic? yielded/paused) virtual threads will ever be resumed (in my case the other vt's in question were Thread.sleeping, but their deadline has long since expired)
>
> :
>
> particulars:
> Java: 19.0.1; OpenJDK 64-Bit Server VM 19.0.1+10
> Runtime: OpenJDK Runtime Environment 19.0.1+10
The implementation of virtual Thread.yield has changed in JDK 20 to 
improve fairness a bit and should reduce surprises. The intention isn't 
to use is to control scheduling but the changed implementation may be 
useful for your teaching.

-Alan


More information about the loom-dev mailing list