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
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
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.
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
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Yes.
Gitesh Ramchandani
Ranch Hand
Joined: Feb 28, 2007
Posts: 274
posted
0
Thanks.
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi Gitesh,
you can see the difference if you modify the sample a little bit.
Gitesh Ramchandani
Ranch Hand
Joined: Feb 28, 2007
Posts: 274
posted
0
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.