我刚刚开始玩 Project Loom ......
鉴于 Java 代码似乎可以正常工作
try (ExecutorService executor = Executors.newVirtualThreadExecutor()) {
IntStream.range(0, 16).forEach(i -> {
System.out.println("i = " + i + ", Thread ID = " + Thread.currentThread());
executor.submit(() -> {
var thread = Thread.currentThread();
System.out.println("Thread ID = " + thread);
});
});
}
当 IntelliJ 将其转换为 Kotlin 时,我得到
Executors.newVirtualThreadExecutor().use { executor ->
IntStream.range(0, 16).forEach(IntConsumer { i: Int ->
println("i = $i, Thread ID = ${Thread.currentThread()}")
executor.submit(Runnable {
println("Thread ID = ${Thread.currentThread()}")
})
})
}
private fun ExecutorService.use(block: (ExecutorService) -> Unit) {}
这似乎编译得很好,但是在执行时,控制台上没有打印任何内容?
Kotlin 和 Project Loom 之间是否存在一些潜在的不兼容性?
进一步的实验表明
Executors.newVirtualThreadExecutor()
.submit(Runnable { println("Thread = ${Thread.currentThread()}") })
不打印任何东西,但是
Executors.newCachedThreadPool()
.submit(Runnable { println("Thread = ${Thread.currentThread()}") })
确实打印了预期的结果,所以 Kotlin 和虚拟线程之间存在根本不兼容的东西。然而,
Executors.newCachedThreadPool().use { executor ->
IntStream.range(0, 16).forEach(IntConsumer { i: Int ->
println("i = $i, Thread ID = ${Thread.currentThread()}")
executor.submit(Runnable {
println("Thread ID = ${Thread.currentThread()}")
})
})
}
不打印任何东西,所以还有其他问题在起作用......确实
Executors.newCachedThreadPool().use { executor ->
IntStream.range(0, 16).forEach(IntConsumer { i: Int ->
println("i = $i, Thread ID = ${Thread.currentThread()}")
})
}
不打印任何东西,但是
IntStream.range(0, 16).forEach(IntConsumer { i: Int ->
println("i = $i, Thread ID = ${Thread.currentThread()}")
})
是否打印预期结果?还,
IntStream.range(0, 16).forEach(IntConsumer { i: Int ->
println("i = $i, Thread ID = ${Thread.currentThread()}")
executor.submit(Runnable {
println("Thread ID = ${Thread.currentThread()}")
})
})
打印预期的结果,所以有一些奇怪的use
?
这引出了问题
- Project Loom 的设计/实现方式是否存在导致 Kotlin 出现问题的问题?
- JDK-18 是否存在一些与 Kotlin 不兼容的问题?
- Kotlin 的设计/实现方式是否存在无法与 Project Loom 互操作的问题?
- Kotlin 是否存在一些与 JDK-18 不兼容的问题?