• 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: 1
  • 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?
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Hello t1" will be printed because t2 is never started.
 
Marshal
Posts: 79961
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…and welcome to the Ranch

Please always use the code button; you can see how much better the code looks because LM has used it.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also the class name.. Its should be meaningful and starts from Capital..
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just so it's clear to the OP, the reason Les's code prints "Hello t1" and not "Hello t2" is because the T2 Runnable instance is ignored by the T1 thread. The constructor that takes a Runnable instance only has any effect if the run() method is not overridden. If you were to remove the run() method in T1 then that code would print "Hello t2".
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read 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