Starting a new thread without specifying a thread group will add it to the default group:
If group is null and there is a security manager, the group is determined by the security manager's getThreadGroup method. If group is null and there is not a security manager, or the security manager's getThreadGroup method returns null, the group is set to be the same ThreadGroup as the thread that is creating the new thread.
The group will keep a reference to the thread as long as it is alive, so it can't be GC'd during that time.
When a thread terminates (= when run()
returns for whatever reason), the thread is removed from the thread group. This happens in the private method exit()
which is called from native code. This is the point in time when the last reference to the thread is lost and it becomes eligible for GC.
Note that the code indicates that ThreadGroup
can be null
but that isn't the case. The various null-checks are just to avoid NPEs in the rare case that something goes wrong. In Thread.init()
, you would get NPEs if Java couldn't determine a thread group.