环境: java21
问题: methods5不会输出log.info的内容, methods6正常打印log.info的内容
private static void methods5() {
ThreadFactory tf = Thread.ofVirtual().factory();
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
Thread vt = tf.newThread(() -> log.info("vt task executed."));
for (int i = 0; i < 5; i++) {
executor.submit(vt).get();
}
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private static void methods6() {
try (ExecutorService executor = Executors.newFixedThreadPool(5)) {
Thread thread = new Thread(() -> log.info("thread task executed."));
for (int i = 0; i < 5; i++) {
try {
executor.submit(thread).get();
} catch (InterruptedException | ExecutionException e) {
// 处理异常
}
}
}
}
正确的做法是给调度器传真正的线程对象,它会以虚拟线程形式运行,而不是给它传虚拟线程,要么你就干脆直接对虚拟线程使用start()方法
改法一:
改法二:
另外虚拟线程没有必要使用池化技术,因为创建和销毁的代价非常小,池化的话属于是违背初心了