• 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

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: 43028
76
  • 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: 43028
76
  • 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.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic