So I have this code excerpt (a minimal reproduction, from a much larger project, and blocking issue). It leverages Java 22 (preview)‘s structured task scope in combo with virtual threads:
playground.java
void main() throws InterruptedException {
final var NAME = ScopedValue.<String>newInstance();
try (var ts = new StructuredTaskScope<>()) {
ScopedValue.runWhere(NAME, "haha", () -> {
ts.fork(() -> {
// ^^^
// java.util.concurrent.StructureViolationException: Scoped value bindings have changed
return null;
});
});
ts.join();
}
}
Using Java 22, you can run it with java --enable-preview --source 22 playground.java
.