Custom scheduler: Using loom for deterministic simulation testing
Alan Bateman
alan.bateman at oracle.com
Mon Jul 7 16:14:22 UTC 2025
On 05/07/2025 21:25, Ryan Yeats wrote:
> :
>
> 1. Its not easy to replace the virtual thread scheduler
> 2. The aforementioned scheduler has no understanding of delays so any
> Thread.sleep or Object.wait introduces continuations back into the
> scheduled execution pool non-deterministically. This affects the
> execution order so bugs can't always be reproduced.
> 3. IO also introduces continuations back to the scheduler execution
> pool non-deterministically.
>
> The first issue can currently be solved by using reflection to make
> the virtual thread constructor public
> <https://github.com/ryeats/loom-dst/blob/main/src/main/java/org/example/SchedulableVirtualThreadFactory.java>. The
> second issue can currently be solved by controlling system time by
> replacing the byte code for all calls to System.nanoTime(),
> System.onCurrentTimeMillis() and Instant.now() using an agent at
> runtime
> <https://github.com/cmu-pasta/fray/blob/main/instrumentation/base/src/main/kotlin/org/pastalab/fray/instrumentation/base/visitors/TimeInstrumenter.kt>.
> The third likely has no general solution but I am interested in
> hearing ideas.
>
There are experimental changes in the loom repo to allow the virtual
thread scheduler be replaced or wrapped. There is also a prototype API
to set a scheduler when creating a virtual Thread. There is a doc in the
repo with more on this [1]. This project hasn't decided whether to
expose anything so any reports from experiments would be useful to hear
about.
-Alan
[1]
https://github.com/openjdk/loom/blob/fibers/loom-docs/CustomSchedulers.md
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20250707/4e3e542c/attachment-0001.htm>
More information about the loom-dev
mailing list