Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
cede()toox.control, next tocheckInterrupt():Virtual threads are not preempted, so a CPU-bound loop can starve other virtual threads (in an experiment with 2x-cores busy-looping VTs, half of them received zero CPU time).
cede()gives compute-intensive code a way to cooperate with the scheduler — and, since it also checks the interrupt flag, with the cancellation protocol: a busy loop callingcede()becomes both fair and cancellable.Why
checkInterrupt + Thread.yieldBased on experiments comparing yielding techniques on virtual threads (JDK 21 & 26, Linux, 8 cores), following up on this gist:
Thread.yieldgenuinely unmounts and resubmits the continuation to the back of the scheduler's queue (external submit when the carrier's local queue is empty) — it reliably prevents starvation (89–93% liveness-probe capture vs 2% baseline), at ~1µs per call.LockSupport.parkNanos(1)gives near-perfect round-robin fairness, but costs ~40–46µs per call (timer round-trip), stalls the caller for that time, and silently degrades to a no-op when the interrupt flag is set — i.e. exactly when the enclosing scope is being cancelled. SinceThread.yieldis ~50x cheaper, calling it more frequently (every ~1ms of computation) matchesparkNanos(1)'s liveness at a quarter of the overhead. The trade-off is mentioned in the scaladoc.LockSupport.parkNanos(0)is a true no-op.Thread.sleep(0)behaves identically tocheckInterrupt + Thread.yield(that's literally its JDK implementation for virtual threads), but only as an@implNote— the explicit version doesn't rely on unspecified behaviour.The interrupt check makes
cede()a cancellation boundary, consistent with e.g. cats-effect'sIO.cede.Includes tests and a docs entry in
utils/control-flow.md.🤖 Generated with Claude Code