• 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 Doubt

 
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 ]
 
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.
 
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.
 
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