File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Difference Between Calling threads using run() and start() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Difference Between Calling threads using run() and start()" Watch "Difference Between Calling threads using run() and start()" New topic
Author

Difference Between Calling threads using run() and start()

Anand Natraj
Ranch Hand

Joined: May 17, 2004
Posts: 53
What is the difference between calling a thread using start() method and using run() method
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
start creates a new thread of execution for your thread to execute in, then the thread is scheduled to run and the run method is called by the system when it's ready to run your thread. start will return as soon as the thread has been spawned.

calling run directly is the same as calling any other method, the calling code must wait for the run method to complete and return.

consider the following code:


the output is:

starting s
s started
s0 s1 s2 s3 s4
running r
r0 r1 r2 r3 r4
r ran

I added s.join() to give predictable results, but it's not necessary, try commenting the try/catch block out.

The point is, s executes in another thread, the calling code can continue while the other thread executes (so to speak). when r.run is called, on the other hand, the calling code must wait for the run method to complete before continuing because it is in the same thread of execution.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Anand!

We've a whole forum dedicated to threading issues. I'm moving this to the Threads and Synchronization forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference Between Calling threads using run() and start()
 
Similar Threads
Difference between run and start method of Thread
Threads...
Class implementing Runnable and calling run() method
Difference between start() and run() method?
Threads