• Post Reply Bookmark Topic Watch Topic
  • New Topic

start vs run

 
Arun Giridhar
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm using java 7



this give ouput (quite random)


now i do run() directly


this give ouput


Can someone help me understand whats happeining
Thank You :/
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridhar wrote:
Can someone help me understand whats happeining


In the first example, your are creating threads to run your codes (in the run() method). In the second example, well, you are not creating any threads. It is still single threaded, as it is the main thread that is running all the codes.

Henry
 
Arun Giridhar
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Arun Giridhar wrote:
Can someone help me understand whats happeining


In the first example, your are creating threads to run your codes (in the run() method). In the second example, well, you are not creating any threads. It is still single threaded, as it is the main thread that is running all the codes.

Henry


1> In the second , why the ids and name are different if it was single threaded.
PS >> Where can i find information on this subject?

 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridhar wrote:
1> In the second , why the ids and name are different if it was single threaded.


Well, you did create lots of thread objects, each with different names and ids. You just didn't start (create) any threads with them.

Henry
 
Arun Giridhar
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assumption

what if the first and second yield same output! and how will you know which is single or parallel ?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridhar wrote:
what if the first and second yield same output! and how will you know which is single or parallel ?


Well, for this example, the easiest way is to print out the ID of the currently running thread (in the run() method).

Henry
 
Arun Giridhar
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my i must have done instead of in run().

Henry Wong wrote:
Well, for this example, the easiest way is to print out the ID of the currently running thread (in the run() method).
Henry


what are the other ways to find ?
 
Zulfiquiar Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you calll



on a thread object you create a new seperately running thread with its own stack , so thats when you actually create a thread of execution.


But when you call



directly on a thread object , you dont have a seperate runnung thread but your run method of the thread object will be execute in the current thread . So no seperate stack for the thread object when you directly call run().


start()-----> creates new thread of execution
run()-------> code is executed in current thread ...no new thread of execution is created.


Note:- start() internally calls run()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!