• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread question - uCertify 6.0

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Eugene Rabii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

reply
    Bookmark Topic Watch Topic
  • New Topic