funky-eyes commented on issue #16042:
URL: https://github.com/apache/dubbo/issues/16042#issuecomment-3788506971
```
public static void main(String[] args) throws InterruptedException {
// Create an executor that spawns a new virtual thread for each task
Executor executor = Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().name("test-", 1).factory());
// Create a virtual-thread-based thread pool with large max size and
SynchronousQueue
// This allows unbounded growth when needed, but reuses idle threads up
to keepAliveTime
Executor executor2 = new ThreadPoolExecutor(
200, // core pool size
Integer.MAX_VALUE, // maximum pool size (effectively
unbounded)
60, // keep-alive time
java.util.concurrent.TimeUnit.SECONDS,
new SynchronousQueue<>(), // handoff queue — forces new thread
creation when pool is busy
Thread.ofVirtual().name("test-threadpool-", 1).factory());
// Warm-up / pre-heat phase to allow JIT compilation and thread creation
for (int i = 0; i < 10100; i++) {
executor.execute(() -> {
// Simulate I/O wait
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
executor2.execute(() -> {
// Simulate I/O wait
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
// Wait long enough to ensure JIT compilation and warm-up are complete
Thread.sleep(5000);
long start = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(5000000);
for (int i = 0; i < 5000000; i++) {
executor.execute(() -> {
// Simulate I/O wait
try {
Thread.sleep(10);
countDownLatch.countDown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
countDownLatch.await();
System.out.println("thread time: " + (System.currentTimeMillis() -
start) + " ms");
start = System.currentTimeMillis();
CountDownLatch countDownLatch2 = new CountDownLatch(5000000);
for (int i = 0; i < 5000000; i++) {
executor2.execute(() -> {
// Simulate I/O wait
try {
Thread.sleep(10);
countDownLatch2.countDown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
countDownLatch2.await(); // Fixed: should await countDownLatch2 (not
the first one)
System.out.println("thread pool time: " + (System.currentTimeMillis() -
start) + " ms");
}
```
Test results are in: the pooled virtual thread pool shows better performance.
```
thread time: 10349 ms
thread pool time: 3364 ms
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]