Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Thread names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread names" Watch "Thread names" New topic
Author

Thread names

Ryan McClain
Ranch Hand

Joined: Nov 27, 2010
Posts: 97
App.java
MyThreadRunnable.java


Why does line 2 of App.java result in a 'null' for the name member of MyThreadRunnable? Is it not so that I just created a new Thread, passed a Runnable to it and gave it a name? Is it not supposed to be so that the name property should carry the value of Thread's getName()?

I don't understand.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2175
    
  47
Sorry but I'm not sure I fully understand your confusion.
Line 2 sets the name field of the thread to what it already is. Why do you think the name field of your Runnable object will have the same name as the Thread?

If you want to set the name field of your Runnable object you need to explicitly set it ie

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

The Thread object and the Runnable object are 2 differrent objects. If you want to set the property of the Runnable object, you need to call the Runnable object's method.
Sourav Ken
Greenhorn

Joined: Oct 11, 2012
Posts: 10
I just tried in a simple way, its working. Hope code is explanatory.



Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2175
    
  47
I just tried in a simple way, its working. Hope code is explanatory.

You have a redundant line in your code:
There is no point in this line as you are setting the name of the runnable1 object to the value returned by runnable1.getName() which is the name of the runnable1 object.

Also it doesn't solve the OP's issue which was to set the MyThreadRunnable object to the same name as the thread. See my earlier post for a solution.
 
jQuery in Action, 2nd edition
 
subject: Thread names
 
Similar Threads
How to make a class thread safe
Need help understanding results from running 2 threads
synchronized blocks for static and non-static methods
Runnable object use
a question on parameter passing