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

Thread execution

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could any please explain how the following program executes and prints the output ?

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

- thanks
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For understanding how the code works you need to know that calling run() on a thread just makes the current thread execute the method run();

calling start() on a thread creates a new thread which will execute the method run();

If you understand this you will know how the code works.
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does not matter if the current running thread is thread who creates and instantiate new thread , so it's gonna be the MAIN thread which will take over the execution of the THREAD class RUN() and goes forward next step as it returns from the RUN().

if it's that then i think i got it, but what if one thread is already running the RUN(). can you please elaborate more on that?

appreciated !
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chintan ramavat..

if it's that then i think i got it, but what if one thread is already running the RUN(). can you please elaborate more on that?



If I am write you are talking about calling run() method from two different threads at the same time..

Ofcourse, that is possible provided run() method is not synchronized..

And about your above query..

When you simply invoke run() method on the instance of a Thread, no new thread is created, main thread itself executes that method, which you can confirm by printing the stack trace..

However when you invoke the native start() method of Thread, it by its implementation, starts a new thread and invoke the run method.. You will have a different stack trace for this thread..

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

I understood your doubt ,I think You are confused about the output of program

that is T1T1T3, this is taken from Dan Questions and As I think You are confussed about the explanation provided by Dan.
I try to clarify your doubts--

First the main() method is calling the start() method by creating a object of Thread so first it will call to target class's run() method(As per Thread constructor) means goes to B class then inside the B's run() method again its calling the A's run() method so first it print T1 after completing the new A().run().It goes for new Thread(new A(),"T2").run(); but this line of code is not calling the start() method but run() method so the second time output will same T1 and third time with new Thread(new A(),"T3").start();
it will calling start() on a thread creates a new thread which will execute the method run();
and it will print T3

So the final output for this code is

T1T1T3
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AtulKumar
Great explaination dude
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

Amir
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic