• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class t1 extends Thread
{
t1(Runnable runnable)
{ super(runnable);
}
public void run()
{ System.out.println("Hello");

}

}

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

class t3
{ public static void main(String args[])
{ t2 T2=new T2();
t1 T1=new T1(T2);
T1.start();
}}
What should be the output..please explain?
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect the output to be a compilation error. Java is case sensitive and you have made some errors with your case when referring to your classes.

Also note that by convention classes in Java should start with a capital letter, and variable and method names should start with a lower case letter.

But assuming you fix the compilation error what do you expect the output to be? Have you tested it to see if you're right?
 
Sanjiv Saha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be some typing errors..but when I compiled it is calling run() of the class extending Thread ..but shouldn't it call run() of the class implementing Runnable because the constructor Thread(Runnable runnable) calls run() of the runnable instance? Please explain.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let's change the System.out.println(...) statements because if they both print "Hello" then you won't know which one was called.

Try it out and see what happens.

Note that the constructor Thread(Runnable runnable) does not call the run() of the runnable instance.
 
Sanjiv Saha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would print Hello T1 but when we pass a reference of a class implementing Runnable to the Thread class directly then the run() of the class implementing Runnable is executed then in this why isn't that happening because a runnable reference is passed to the Thread class ..this is my doubt..please explain
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as you understand, when a thread is started its run() method is called, so let's have a look at the contents of run() in Thread...



'target' is the Runnable instance that was passed to the Thread in its constructor. So as you can see, Thread's run() method will always delegate to the Runnable instance that it was constructed with.

However in the case of your class t1 (which should be called T1), the run() method in Thread is not being run. You have overridden it, so the run() method in t1 is run instead.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!