ayushgp
ayushgp

Reputation: 5091

When do references to threads in java go out of scope?

When we define new threads in Java, when do the thread's references go out of context? For example, if I create a WeakHashMap<Thread, Object> when would an entry of a dead thread be dropped from this Hashmap?

Also in case a thread pool is used to execute some task(Runnable), will that reference from the hashmap be ever dropped?

Upvotes: 1

Views: 160

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96394

A thread is a garbage collection root as long as it is alive. Once it terminates it would be eligible for dropping from the weak hash map.

For the second part, reference to what? If reference to the Runnable task, the reference held by the threadpool will go away once the pool completes executing the task. The threads in the pool will keep executing tasks until the pool is shutdown or until they die due to an uncaught exception.

Upvotes: 2

Related Questions