wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tough Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tough Thread question" Watch "Tough Thread question" New topic
Author

Tough Thread question

Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}
}
Can any one help me understand what is happening in this code> and waht will be output?
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Originally posted by Preeti:
class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}
}
Can any one help me understand what is happening in this code> and waht will be output?


here first in the main method object of thread class ic created by passing an object of runnable interface which is of class B and thread name.
then start method is invoked. now method ckecks class of current object while running. so it will call run method in B.
Now in B again run method in A is called with same thread.now again run method is called but as i said run method doesn't create new thread so with same thread run method in A is called i.e. with T1 thread.
then again start method is called so this time it will create new thread.
i.e. T3 and then it will call run method in class A
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Originally posted by Preeti:
class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}
}
Can any one help me understand what is happening in this code> and waht will be output?

so here ur output will be
T1T1T3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tough Thread question
 
Similar Threads
Thread Question
doubt in wait() method.......
threads
Thread execution
Thread