Mridul Vishal
Mridul Vishal

Reputation: 2310

Thread initialized to null in java

public class ThreadState {

    public static void main(String[] args){
            Thread t = new Thread(){
            public void run(){
                       // infinite loop
                       while (true) {
                        try {

                         Thread.sleep(1000);
                        }
                        catch (InterruptedException e) {
                        }

                        System.out.println("thread is running..."+Thread.currentThread().toString());
                       }

            }
        };
        t.start() ; 
        t = null ;
        while (true) {
              try {

              Thread.sleep(3000);
              }
              catch (InterruptedException e) {
              }
              System.out.println("thread is running..."+Thread.currentThread().toString());
        }   

    }
}

Thread instance t is initialized to null .. still it is able to run and prints its detail on the console . Need an explanation for this

Upvotes: 3

Views: 4023

Answers (5)

MobileEvangelist
MobileEvangelist

Reputation: 2628

I think Jon is correct i.e. "Changing the value of the variable doesn't affect the existing Thread object at all."

But actual explanation you required is

In programming method that uses static or global memory local to a thread. Because normally all threads in a process share the same address space, which is sometimes undesirable. In other words, data in a static or global variable is normally always located at the same memory location, when referred to by threads from the same process. Each thread has its own stack.

Hope the conceptual explanation given is helpful.

Upvotes: 0

Fredrik
Fredrik

Reputation: 5839

You set t to null, that doesn't do anything with the thread itself, it just assigns null to the variable where you stored a reference to the instance.

Maybe this will help:

What if you do

Object t = new Thread() {...}
t.start
Thread t2= (Thread)t;
t="Orange";

Would you expect magic things to happen then as well? What if you passed t to another method instead of storing another reference in t2?

t is just a placeholder for a reference to the thread, assigning something to t has just as much impact on the thread as assigning null to a below has to the number 2.

Integer a = 2;
a=null;

Upvotes: 1

Gray
Gray

Reputation: 116878

@JonSkeet and others have explained well that then you set t = null you are not changing the running Thread at all, you are just changing the variable t. Once the thread has started, the JVM manages the thread so even if there are no references to it, it will still run and not be GC'd.

Another thing to point out is that, in the loop that follows you are doing a:

System.out.println("thread is running..."+Thread.currentThread().toString());

This is not printing out the status of the thread that you just forked but rather printing out the current thread -- the thread that did the forking. This is the "main thread" that runs the main() method. The main thread can finish and the thread that you forked will continue to run -- keeping the JVM from exiting. Only if the thread that you forked is marked as being a daemon thread will the JVM finish when the main thread finishes.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500495

Thread instance t is initialized to null

No, the Thread variable is set to have a null value. Variables aren't instances - it's worth making absolutely sure that you understand that.

Changing the value of the variable doesn't affect the existing Thread object at all.

Upvotes: 11

SLaks
SLaks

Reputation: 887433

t = null; just removes a reference to the Thread instance.
It doesn't affect the thread itself.

In particular, an executing Thread instance will never be GC'd.

Upvotes: 5

Related Questions