Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.284 :
Posted: Wed Mar 05, 2014 2:03 pm
by devlam
Thread class's run() is an interesting method. If the thread object was constructed using a separate Runnable object, then that Runnable object's run method is called otherwise, this method does nothing and returns.
I have trouble with the otherwise part.
If I read that it looks if you are saying that the run method isn't invoked or...
But if I create a class extending Thread with an overriding run method and then call that run method on a instance of that class that run method is actually doing what's inside that run-method.
Only if I don't override the run method in a class extending Thread calling run on an instance of that class nothing will happen.
Re: About Question com.enthuware.ets.scjp.v6.2.284 :
Posted: Wed Mar 05, 2014 9:01 pm
by admin
The explanation is talking about the run method of Thread class, not about the run method of the class that might extend Thread and override the run method.
So you pass a Runnable instance while creating a Thread instance, then Thread class's run() invokes Runnable's run(), otherwise, it does nothing. Here is the code for Thread class's run method:
Code: Select all
public void run() {
if (target != null) {
target.run();
}
}
target is the reference to a Runnable that you pass while constructing an instance of Thread.
Re: About Question com.enthuware.ets.scjp.v6.2.284 :
Posted: Thu Mar 06, 2014 5:02 am
by devlam
So actually it should be explained as:
If the thread object was constructed using a separate Runnable object, then that Runnable object's run method is called;
If the thread object was constructed from a extended Thread-class, then if there is an overriding run method that run method is called;
Otherwise the run method of the Thread-class itself will be called which does nothing.