• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Qn on Threads.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Answer is e,can anybody explain??
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the run method is called directly, the method simply executes in the current thread instead of as its own thread.

In this example, the thread T1 directly calls run on two new instances of A, so both of these report the current thread as "T1." Then the start method is called on T3, which allows T3 to execute as its own thread.

So the output is T1T1T3.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CS-

I think it has to do with the fact that the threads are being started with run() instead of start(). If you call the run() method directly it starts the thread in the current thread. If you call start() it spawns a new thread to run in.

The new A().run() prints T1 because that's what it was named in the main method of class C.

The new Thread(new A(),"T2").run(); prints T1 because it's actually run in the same thread (the T1 thread started by the main method of class C). I would assume the "T2" name is just dropped on the floor since it's going to run in an already existing thread that already has a name of T1.

The new Thread(new A(),"T3").start(); prints T3 because it's actually a new thread since start() is used instead of run().

Josh
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc and I must have been typing our posts at the same time. This has happened in a few other threads as well.

No worries.

Josh
 
cs singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to both of you !! i got the point.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic