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

Thread question from one of Dan's exam

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain why "A" is printed instead of "B"? Thanks.
class A extends Thread {
public A(Runnable r) {super(r);}
public void run() {System.out.print("A");}
}
class B implements Runnable {
public void run() {System.out.print("B");}
}
class C {
public static void main(String[] args) {
new A(new B()).start();
}
}
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
start() is a call on A not B. so it calls A's run. hence A as output
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think following is happening:
1. An object of class B is created, without any reference to it.
2. This object is passed to the constructor of class A. An object of class A is created without any reference to it.
3. start method on object of class A is called. That will call run() in class A which will print "A"
Hope this helps.
Barkat
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathy
Well this is really a nice question. This question not only relates with threads but also with method overriding.
new A(new B()).start(); this should cause thread A to be started and run the run() method. But which one A's or B's. Well it should be B's run method that should be called as the you have passed an object of B in the constructor of thread A.But this doesn't happens. Because when new A(new B()).start(); is called it invokes Thread's class start() method or you can say A's start method as A also has inherited it. This start methods points towards the run method of the Orignal Thread class. Which might be something like


Now this method is overidden by A(as A has redefined it) so now instead of executing the Thread's class run method which would have caused B's run() to be called A's run() method is called. You can check this by commenting out the A's run() method which will now cause the B's run method to run.
To put it simply the orignal Thread class method will call the Runnable's run() method but in case you override it the overriden method will be executed.
In case if its still not clear, ask again.
Well I also had the same problem and Maulin helped me out. You can check out this thread which covers this topic. Thread
[ May 22, 2003: Message edited by: Anupam Sinha ]
 
Kathy Wong
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam Sinha, thanks for the detailed explanation. It has really cleared things up for me.
Regards,
Kathy
 
reply
    Bookmark Topic Watch Topic
  • New Topic