Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

thread  RSS feed

 
Barbara Nutter
Greenhorn
Posts: 1
  • 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?
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Hello t1" will be printed because t2 is never started.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • 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.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the class name.. Its should be meaningful and starts from Capital..
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • 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".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!