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()?
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
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: 8
posted
0
I just tried in a simple way, its working. Hope code is explanatory.
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.