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
No, the
Threadvariable 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
Threadobject at all.