• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic