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
MyRunnable r = new MyRunnable();
you are creating new instance of class MyRunnable
which mostly implements Runnable
interface:
2- At
Thread t = new Thread(r);
you are creating a new thread and passing by value the object that the reference r is pointing to:
3- At
r = null;
you are removing the link between the r reference and the MyRunnable
object, which Thread t
is using to run the thread:
No implications.
The Thread has already started. And regardless, the Thread Object will be holding onto the reference so it will not get garbage collected until t
does.
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