-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caffeine pins the carrier thread of virtual threads #779
Comments
Please try using |
Thanks, the following style is working: @Test
public void foo() {
// compile and run with --enable-preview --add-modules jdk.incubator.concurrent -Djdk.tracePinnedThreads=full
var cache = Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.executor(Executors.newVirtualThreadPerTaskExecutor())
.buildAsync(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
try {
// Simulate IO with sleep
Thread.sleep(5000L);
return "foo";
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
for (int i = 0; i < 2; i++) {
scope.fork(() -> cache.get("foo").join());
}
scope.join();
scope.throwIfFailed();
} catch (Exception e) {
throw new RuntimeException(e);
}
} |
|
Unfortunately the only reliable solution will be when virtual threads support object monitors. The recent Loom EA builds handle this, so maybe JDK 25 will rectify this problem. |
When running on virtual threads, one must not be doing blocking IO in synchronized blocks, or in a native function. However,
Caffine
usesConcurrentHashMap
's compute function, which operates with synchonized blocks. The following code yields a carrier thread pin event:The text was updated successfully, but these errors were encountered: