About Question com.enthuware.ets.scjp.v6.2.284 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
devlam
Posts: 50
Joined: Sun Nov 10, 2013 4:39 am
Contact:

About Question com.enthuware.ets.scjp.v6.2.284 :

Post 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.

admin
Site Admin
Posts: 10398
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.284 :

Post 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.

devlam
Posts: 50
Joined: Sun Nov 10, 2013 4:39 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.284 :

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests