• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

thread

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic