This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread question - uCertify 6.0

 
Eugene Rabii
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic