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

threads

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}}


Please explain me why the o/p here is T1T1T3
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads are invoked by calling a start() method not by calling run() method.
Means when you call Thread.start() new thread is created.

So You are setting the name of the thread as T1
After exection of
new Thread(new B(),"T1").start();

new thread is generated with name "T1" and B's run metod will be invoked(becz of start())
within this thread u r calling
new A().run(); //output "T1"

this causes A's run() method to call (not a seperate thread) with thread "T1".

new Thread(new A(),"T2").run(); //output "T1"

Tis line again calling A's run() method within same thread "T1".

new Thread(new A(),"T3").start(); //output "T3"

In this line u r creating new thread(becz of start()) with the name "T3".
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Its because you call directly the run()...so It doesn't create another thread and you get T1(from new Thread(new B(),"T1").start(); )&&(from new A().run(); )
T1(new Thread(new A(),"T2").run(); )
T3(from new Thread(new A(),"T3").start(); )
Im also not sure this is guaranted...but im not a thread specialist...
arno
[ May 31, 2006: Message edited by: Arno Reper ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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();
}}


Please explain me why the o/p here is T1T1T3



new A().run(); and new Thread(new A(),"T2").run(); did not create new thread, they only print out the current thread name "T1"

after new Thread(new A(),"T3").start(); new thread "T3" is created.
 
eat bricks! HA! And here's another one! And a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic