Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

new Thread(x).run(*)?!  RSS feed

 
Roger Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all

If input the following code into line 3, the run() method also excutes successfully!

Here, the thread did not start to run without calling start(). So what is the fact? Is there anything different from the codes here:


Thanks in advanced,
Roger
[ September 09, 2003: Message edited by: Roger Zhao ]
 
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
run() is just a plain, ordinary method. If you call it, it will run. This shouldn't surprise you. Nothing stops you from calling it, just like nothing stops you from calling a main() method. It's just that run(), like main(), has a special role to play, by convention.
Thread.start(), on the other hand, is a magical method. If you call it, then it will find a run() method to call, and call it in a separate, parallel thread of execution, different from the one that called it. When start() returns, run() is running in one thread, and the caller in another.
How does Thread.start() finds a run() to call? It calls Thread.run() on that same Thread object. One of two things will be true: either you overrode run(), in which case start() will be calling your code, or you provided a Runnable object. If you provided a Runnable object, then start() will call Thread's default run() method, which will call your Runnable's run() method, so it will again be calling your code.
So if you call Thread.run() directly, it calls your Runnable's run() method. This is not a big surprise. It isn't called on a new parallel thread, though; it's called synchronously, just like any other method call.
[ September 09, 2003: Message edited by: Ernest Friedman-Hill ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!