• 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
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Dan's thread question!!

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

Ans is T1T1T3
In the main method, new Thread(new B(),"T1").start(); how does it know which run() to invoke?? that of class A or that of class B?
Also how did the control go to new Thread(new A(),"T3").start();
Edited by Corey McGlone: Added CODE Tags
[ March 17, 2004: Message edited by: Corey McGlone ]
 
Sandhya Harish
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I understand that a new thread is started only with the start() method. therefore the stmt in main starts a thread "T1", which is printed because the run() in class A is invoked??
After that, in class B:
new A().run(), does not start a new thread, but only invokes the method run(), Thread.currentThread returns "T1"
new Thread(new A(),"T2").run();, once again calls the run() method in class A without creating a new thread and Thread.currentThread returns "T1"

new Thread(new A(),"T3").start();, here the start() invokes the run() in class A starting a new thread "T3" which gets printed.
Am i right? or is my logic in understanding this totally wrong? please help!!
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I can see, your logic is quite OK.

But what bothers me is this line:

Shouldn't this invoke the run() method in the class Thread?? Or it's somehow "short-circuited" to invoke the new A().run() (I mean it obviously is but I'd appreciate a more technical explanation if someone can give it )?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From javadoc :

public void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.


Before any question rtfm, it's your base of knowledge.
Good luck.
 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is that it's not quite clear, because after this

I indeed have a brand new Thread object - but nevertheless is not "true" thread - it still is NOT a separate thread of execution.
So what's the "thread" in the quote - Thread object or a real thread of execution? Obviously it's a bit vague.
Anyways, thanks for your time.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key to understanding that line is that by invoking the run() method directly, we are not creating a new thread of execution. Rather, we're just invoking a method like any other.
In that case, when you invoke the method Thread.currentThread(), you are returned a reference to the thread that invoked the run() method - in this case, that thread is "T1."
When you invoke the start() method, a new thread is spawned and that thread invokes the run method. Therefore, when you invoke Thread.currentThread(), you obtain a reference to that brand new thread, not the one that invoked the start() method.
I hope that helps,
Corey
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


,
here you are specifying that Runnable implementation B is the class
whose run method should be invoked.
The thread name is specified as "T1".
2) B's run method is like :

Keep in mind once you started B, the current thread name is T1. Untill another thread is started which happens only in line 3.
line 1 : calls the run method of A. Current thread = T1 => output T1
line 2 : calls the run method of A keep in mind this is not same as starting the thread. Current thread = T1 (still) => output T1
line 3 : creates a new Thread passing runnable A and sets the Thread name to T3, and starts it. Now the current thread is this new thread with name T3. => T3.
Hope that helps.
Thanks,
Rumpa
[ March 17, 2004: Message edited by: Rumpa Giri ]
[ March 17, 2004: Message edited by: Rumpa Giri ]
 
Sandhya Harish
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Rumpa.
I guess that was the nagging doubt I had, as to which object's run() was being invoked.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic