aspose file tools*
The moose likes Threads and Synchronization and the fly likes why start() to creat and run thread ,why not only run() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "why start() to creat and run thread ,why not only run() method" Watch "why start() to creat and run thread ,why not only run() method" New topic
Author

why start() to creat and run thread ,why not only run() method

Sachin Yewale
Greenhorn

Joined: Feb 02, 2012
Posts: 21
Hi i know the basic difference of start() method and run() method of Thread class.My question is why java has given two method for creating and running thread why not only one method for crating another thread that is run() so that we can call directly run to create new Thread (with new call stack ) rather than take a round trip from start() method
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

Because if you call run method on a Thread from your main thread, the run method will execute in the main thread, not the new thread. The Thread class represents a native thread in the OS. Java needs to make some native calls to start the native thread before it can execute the run method in the native thread. start method contains the native calls to start the native thread.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
There are two different methods to capture two different meanings hence the two different method names. start executes native code to request and schedule a thread from the OS resources while run executes the developer's java code.
One method that does both would have given the wrong impression, lumped unrelated concerns in the same method and required at least an extra proxy for the JVM to be able to perform all that magic.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What the others say is true, but I still think there is a failure in the API. The Thread class should not (in my opinion) implement Runnable and should not have a public run() method at all. If we could start over I would choose to have a Thread class with the start() method and no run() method so as to:
1) avoid this confusion between start() and run()
2) reduce the number of times Thread class gets sub-classed just to provide a run() implementation (use a Runnable).

But it's too late to re-start now.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why start() to creat and run thread ,why not only run() method