• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt on thread  RSS feed

 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in threads, to invoke a run method we are using the stt like the one below

instance.start();

why we don't do like

instance.run();

is there any spl. reason for this?

or is the start() implicitly create a separate process and inturn call this.run() (This is my guess)

Can one come with the right reason?

---------------------------
Thanks in Advance

Regards
Thanigaivel S.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your guess is exactly right. run() is just an ordinary method, but start() is a magic one, with a native code implementation that knows how to call run() in a parallel coprocess.
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happens after you call start()?
■ A new thread of execution starts (with a new call stack).

■ The thread moves from the new state to the runnable state.
■ When the thread gets a chance to execute, its target run() method will run.

There�s nothing special about the run() method as far as Java is concerned. Like main(), it just happens to be the name (and signature) of the method that the new thread knows to invoke. So if you see code that calls the run() method on a Runnable (or even on a Thread instance), that�s perfectly legal. But it doesn�t mean the run() method will run in a separate thread! Calling a run() method directly just means you�re invoking a method from whatever thread is currently executing, and the run() method goes onto the current call stack rather than at the beginning of a new call stack

cheers
Rajesh
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!