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

Simple Doubt with Threads

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi for the following code

public class A extends Thread {

public void run{

System.out.print("RUNNING");
}

public static void main(String[] args) {
A a = new A();
a.start(); //1
a.run(); //2
}

}

My doubt is, both 1 and 2 display RUNNING. so is it not true that calling run will actually call the run method in Thread class?

Regards,
Gitesh
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so is it not true that calling run will actually call the run method in Thread class?



Calling run doesn't "call run", it simply executes the run method. Calling start will in turn lead to the run method being called.

The difference is that calling start will actually start a new thread, while calling run will execute the code in the same thread as the main method runs in.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:


Calling run doesn't "call run", it simply executes the run method. Calling start will in turn lead to the run method being called.

The difference is that calling start will actually start a new thread, while calling run will execute the code in the same thread as the main method runs in.



So if I'm asked in which case (out of 1 and 2) will a new thread of execution start, my answer should be a.start(); //1. Am i correct now?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh,

you can see the difference if you modify the sample a little bit.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
Hi Gitesh,

you can see the difference if you modify the sample a little bit.



Thanks Manfred,

I see that when run() is directly called, no new thread is created. Only by calling start a new thread is created.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic