• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Thread Doubt

 
giribabu rosi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A extends Thread {
public A(Runnable r) {super(r);
run();
}
public void run() {System.out.print("A");}
}

class B implements Runnable {
public B()
{
run();
}
public void run() {System.out.print("B");}

}

class Cero {
public static void main(String[] args) {
new A(new B()).start();
}
}// why it is printing BAA
// first call should go to super class only
[ May 27, 2005: Message edited by: giribabu rosi ]
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


tell why it is priting A only suppose to print BA Know??



Because the statement new A(new B()).start(); (see brackets carefully) is tends to calling start method on class A.
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you have started only one thread
i.e.,
new A(new B()).start(); will start only thread A.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling the start method(which is used to spawn a new thread) only on the instance of class A and not on class B. So the result will be only A.
 
reply
    Bookmark Topic Watch Topic
  • New Topic