After a thread started, if the reference of the thread is set to null
, what will happen to the thread? Does it stop? Is it eligible for GC?
Like this:
t.start(); t = null;
The java. lang. NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.
Dead. A thread can die in two ways: either from natural causes, or by being killed (stopped). A thread dies naturally when its run() method exits normally. For example, the while loop in this method is a finite loop--it will iterate 100 times and then exit.
start. Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.
Live thread will continue running even its reference is set to null.
Just like any other object, when there are no references to it, it is eligible to GC. The tricky point is that a running thread has a reference in a ThreadGroup even if your program does not retain one, thus a running thread is never GCed.
what will happen to the thread?
Nothing.
Does it stop?
No.
Is it eligible for GC?
No.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With