Not resuming Virtual Threads?
Alan Bateman
Alan.Bateman at oracle.com
Fri Jul 3 07:15:13 UTC 2020
On 03/07/2020 03:06, David Holmes wrote:
>
> A Thread sleeping forever does not allow the Thread or its Runnable or
> any other reachable object to garbage-collected. Can you please
> clarify what the lifecycle of a virtual thread is and how that relates
> to its reachability?
>
> For reference a regular Thread is always logically reachable from its
> ThreadGroup, until it terminates. And ThreadGroups with active threads
> are reachable from their parent ThreadGroups. Hence all live threads
> are always reachable.
A virtual Thread is just an object in the heap. It will always be
reachable when mounted of course. If it parks waiting on a lock then
it's the lock's waiter list that keeps the Thread reachable. It it parks
in a blocking I/O operation then there is equivalent that keeps the
Thread reachable so it can be unparked when the I/O operation can
continue. If it sleeps then it will be scheduled to be unparked after
the sleep time so that keeps the Thread reachable when sleeping. If
there is code executing in a virtual thread that uses LockSupport.park()
when it will be GC'ed if there is anything the Thread reachable.
Regular thread are GC roots in that their stacks are scanned for
references. You are correct that the ThreadGroup also keeps Thread
objects reachable too but we are on a mission to deprecate and
eventually remove most of the TG API. This includes getting rid of the
internal "threads" array. It will take a few releases and there will be
interim stages where legacy APIs (such as "enumerate" methods) will work
by reconstituting group membership from the thread list in the VM. JVMTI
will do the same.
-Alan
More information about the loom-dev
mailing list