aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question - uCertify 6.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question - uCertify 6.0" Watch "Thread question - uCertify 6.0" New topic
Author

Thread question - uCertify 6.0

Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Given the code below:

public class Color extends Thread {

public static void main(String [] args){

new Color(new MyRunnable()).start();

}
public void run(){
System.out.println("Color run");
}
public Color(Runnable runnable){
super(runnable);
}

}
class MyRunnable implements Runnable{
public void run(){
System.out.println("MyRunnable run");
}
}

And the posibilities:
A. Compilation fails
B. Runtime Exception
C. Color run
D. MyRunnable Run

The answer is C (I tried it and it's really C). But what I do not understand is: "the Target" is MyRunnable how come it's run method is not getting called? What am i missing here(again that is )

thank you,
Eugene.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Eugene, this happens because you are using your custom Thread subclass. The run method in your class doesn't do anything with the target runnable. The run method of the Thread class does something like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Thank you Ankit,
Your explanation makes some seance to me, but have to be very careful if I get a question like this - truly hope I will remember this on the test day.
Ankit Garg wrote:Eugene, this happens because you are using your custom Thread subclass. The run method in your class doesn't do anything with the target runnable. The run method of the Thread class does something like this

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi Eugene,

just remember that default implementation of Thread.run() method simply calls run() method of the Runnable object
passed to it with the Thread(Runnable r) constructor.
If you create thread with Thread() constructor, default implementation of Thread.run() does nothing.

But if you override (replace) the run() method of the Thread class, then your run() method is called instead of default Thread.run()
- even you create this Thread with Thread(Runnable x) constructor.
In your run() you can still call default implementation of Thread.run() using super.run().

In the question class Color (which extends Thread) implements its own run() method (overrides/replaces default Thread.run() method),
so when you create Color object and call start() on it - its own run() method is called (not overriden Thread.run()) - so "Color run" is printed.
Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
I like this explanation too, but just to be clear I am not confusing anything...
So this is just about if/if-not the run method is overridden in a Child class of the Thread class? If yes then this is a very good explanation and easy to remember on the day of the exam.
P.S. I love this forum already.
Thank you,
Eugene.
Ireneusz Kordal wrote:Hi Eugene,

just remember that default implementation of Thread.run() method simply calls run() method of the Runnable object
passed to it with the Thread(Runnable r) constructor.
If you create thread with Thread() constructor, default implementation of Thread.run() does nothing.

But if you override (replace) the run() method of the Thread class, then your run() method is called instead of default Thread.run()
- even you create this Thread with Thread(Runnable x) constructor.
In your run() you can still call default implementation of Thread.run() using super.run().

In the question class Color (which extends Thread) implements its own run() method (overrides/replaces default Thread.run() method),
so when you create Color object and call start() on it - its own run() method is called (not overriden Thread.run()) - so "Color run" is printed.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Isn't it Thread.start() calls the run method of the Runnable? I thought public void Run() is a method of the Runnable Interface not the Thread class (though Thread implements Runable.

For example




If I was to change the code to this.



It still has the same output, but if I am correct in thinking this does not create a new Thread instance, and is simply running a method of the class MyThread, and nothing else.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
 
Consider Paul's rocket mass heater.
 
subject: Thread question - uCertify 6.0