Java Virtual Thread
Coming to Java 19: Virtual threads and platform threads
virtual thread will mount
on platform thread, to better exploit platform threads
when virtual thread is blocking, it will be unmounted
from its carrier thread
.
some times, virtual thread will pin
or capture
their carrier thread
, in which won't be unmounted
.
do not pool
virtual thread, instead use semaphores to limit the number of virtual threads doing the same type task.
do not pin
or capture
their carrier thread
. eg: Reentrant Locak instead of Syncronized
Reconsider the use of thread local
variables, because virtual thread
can be numerous.
virtual thread
can support structured concurrency
to avoid thread leak
Q: When virtual thread
unmounted
from carrier thread
, where the stack frame will be store?
A: heap
Q: How JDK decides which virtual thread
to mount
?
A: FIFO
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)12.4.1 线程的实现
Q: Why virtual thread
is cheaper?
A: platform thread
is 1:1 mapping to LWP(OS Thread), need more space, need switch between user space and kernel space. virtual thread
is M:N(CORRECT ?) mapping to user thread
, smaller and cut of lots of kernel operations.
Thread at OS level?
Thread at hardware level?