This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Thread set as Null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread set as Null" Watch "Thread set as Null" New topic

Thread set as Null

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 124
For the following sample code

After t= null; I guess the thread will still run? but not sure if there is other efftect for set thread as null. Any comments for that?

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I wouldn't set it to null unless setting it to null had some special purpose in the code.
Otherwise it serves no real purpose and worse could be misleading.
Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 124
Thanks, and yes, it is not a normal and good way to do it. But I also hope to understand what is the result ( or no affect ) with this way for thread.

Sriram Naga

Joined: Aug 24, 2010
Posts: 8
The thread gets executed anyway. But you can't refer to that thread object once you assign it to null. You will get a Null Pointer exception if you do so.

[My Page] | [JavaRanch FAQ] | SCJP6

"I am not afraid of the storm, as I am learning to sail my ship"
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I think, there should be an internal reference to the threads. So, nothing will happen, thread terminates normally.

Try to invoke this code in your thread's run method.

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
I agree. Here's the link:
subject: Thread set as Null
It's not a secret anymore!