The virtual thread is not parked in InetAddress isReachable
Anton Rameykov
a.rameykov at gmail.com
Tue Apr 22 12:18:25 UTC 2025
Good afternoon, everyone! I think I've found a bug. There is a code that
basically pings the server. I decided to test it on virtual threads and for
some reason they don't work in it.
Code:
```
public static void main(String[] args) throws ExecutionException,
InterruptedException {
System.out.println("Test fixed thread poll");
long start = System.currentTimeMillis();
var counter =
ping(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
System.out.println("Execute time " + (System.currentTimeMillis() -
start) + " " + counter);
System.out.println("half-time break");
LockSupport.parkNanos(10000000000L);
System.out.println("Test virtual thread poll");
start = System.currentTimeMillis();
counter = ping(Executors.newVirtualThreadPerTaskExecutor());
System.out.println("Execute time " + (System.currentTimeMillis() -
start) + " " + counter);
}
private static int ping(ExecutorService executor) throws
ExecutionException, InterruptedException {
AtomicInteger counter = new AtomicInteger();
var futures = Stream.<CompletableFuture<Void>> builder();
for (int i = 0; i < 1000; i++) {
var fut = CompletableFuture.runAsync(() -> {
try {
InetAddress address = InetAddress.getByName("8.8.8.8");
boolean reachable = address.isReachable(100);
counter.incrementAndGet();
} catch (Throwable e) {
e.printStackTrace();
}
}, executor);
futures.add(fut);
}
CompletableFuture.allOf(futures.build().toArray(CompletableFuture[]::new)).get();
return counter.get();
}
```
I get the following results. (-Djdk.tracePinnedThreads=full is enabled and
it outputs nothing to the console)
Test fixed thread poll:
Execute time 6317 1000
Test virtual thread poll:
Execute time 6319 1000
I expected to get results like this. (You can get such results if you
replace the request to the network with Thread.sleep(100))
Test fixed thread poll:
Execute time 6308 1000
Test virtual thread poll:
Execute time 110 1000
openjdk 22.0.2 2024-07-16
OpenJDK Runtime Environment GraalVM CE 22.0.2+9.1 (build 22.0.2+9-jvmci-b01)
OpenJDK 64-Bit Server VM GraalVM CE 22.0.2+9.1 (build 22.0.2+9-jvmci-b01,
mixed mode, sharing)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250422/d2d64399/attachment.htm>
More information about the core-libs-dev
mailing list