• 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,
[ September 09, 2003: Message edited by: Roger Zhao ]
author and iconoclast
Posts: 24204
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic