• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt on Thread Question

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
here is the question whose answer was not clear to me.
Can someone explain me?
The code goes like this:

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

the answer to this is:T1T1T3
whereas i was expecting it to T1T2T3
please explain....
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer will be T1T1T3 only because calling the run method on a thread
doesnt create a new thread.
Only if you call the start method of a thread it will start a new thread.
[ May 16, 2007: Message edited by: Tina Tibrewal ]
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tina,
Thanks for the explaination.
Can you be bit more elaborate on this through some examples?
 
Tina Tibrewal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok here goes the example:



The output will be mainT1.
When you call run() Directly it will call it from the main thread.
But when you call start a new thread called T1 will be started.

I hope that makes things clear.

Regards.
 
That feels good. Thanks. Here's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic