say I do the following…
//MyRunnable is a class that I have declared, which implements Runnable.
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
r = null;
What are the implications of setting r to null like I have in the above code snippet ?
Let me explain this to you via figures:
1- At
you are creating new instance of class
MyRunnablewhich mostly implementsRunnableinterface:2- At
you are creating a new thread and passing by value the object that the reference r is pointing to:
3- At
you are removing the link between the r reference and the
MyRunnableobject, which Threadtis using to run the thread: