Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread names

 
Ryan McClain
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried in a simple way, its working. Hope code is explanatory.



 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic