This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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.
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.
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).