• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sachin Yewale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic