Java : What happens if a Runnable that is being used in a thread is set to null?
Asked Answered
L

3

7

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 ?

Lionhearted answered 13/6, 2012 at 9:12 Comment(2)
+1 Take a look at my answer below :)Spark
You are welcome. Also, take a look at my answer here: https://mcmap.net/q/18489/-java-pass-by-reference/…Spark
S
8

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:

enter image description here

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:

enter image description here

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:

enter image description here

Spark answered 13/6, 2012 at 9:33 Comment(0)
O
6

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.

Overprint answered 13/6, 2012 at 9:13 Comment(2)
No implications even if the Thread is not yet started, because the Thread itself has a handle on the Runnable too...Limey
@Limey True. I'll clarify.Overprint
D
3

Nothing.

The variable r is not the MyRunnable object itself; it's merely a reference to that object within your block of code.

Initially you create a new MyRunnable object. And then, you "give it the name"/assign it to the variable r. You then pass it into the Thread constructor (using the variable to describe which object you're talking about). Within that constructor, it will almost certainly be assigned to other references (in the JDK I'm using, it's a field called target).

Later on, you repoint your reference r at another object - in this case, the null object. This does not have any effect on the object it used to point it, just the reference. Likewise it does not have any effect on other references pointing to the same object.

So the Thread.target reference still points at the same MyRunnable object you initially created, and from the thread's viewpoint, nothing has changed.

The only potential difference is that your (outer) code snippet no longer has a reference to the object it created. So the code that follows will not be able to call any methods on that object, or pass it as a method argument etc. (This shouldn't be a problem - or surprising - given that you deliberately nullified your only reference to that object!)


If nothing holds a reference to a particular object, then the garbage collector will on its next run consider that object unreachable, and collect it. This is seldom something you need to worry about though, since if you don't have a reference to the object you wouldn't be able to do anything with it anyway (the whole principle behind GC).

In this case, the MyRunnable won't be GCed because the Thread still holds a reference to it.

That said, if the constructor were to behave differently and not store a reference because it didn't need one (maybe it just uses the toString() representation), then the object would be deemed unreachable, and would be collected. In both cases the garbage collector would do the right thing - collecting an object if and only if nothing refers to it any more - without you needing to worry or know about that in your code.

Dryly answered 13/6, 2012 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.