• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

'start' and 'run' method - behaviors!!!

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A Thread(say, t) that has been issued a "t.start()" runs the thread once and completes the thread to 'dead' state. Now, when we again issue a "t.start()", there is no compiler error but a IllegalThreadStateException is thrown at runtime. My Questions are:

1. When the 'start' method calls 'run' internally, why does multiple 't.run()' methods when issued on the code, doesnt produce the same exception, BUT RUNS THE CODE PERFECTLY?

2. Javac being very efficient COMPILER, why CAN'T the compiler developers trap this error at compile time itself?
[ October 04, 2004: Message edited by: Manikandan Jayaraman ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manikandan Jayaraman:
1. When the 'start' method calls 'run' internally, why does multiple 't.run()' methods when issued on the code, doesnt produce the same exception, BUT RUNS THE CODE PERFECTLY?



Because calling run directly doest execute it in a different thread.


2. Javac being very efficient COMPILER, why CAN'T the compiler developers trap this error at compile time itself?



Because in many cases it is simply not possible to know at compile time.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mani and Ilja,

As a continuation of this discussion, say an extension, I think i am right in saying that the Dead thread cannot be started by a thread.start() method.

Instead, under such circumstances, we need to pass it to a new object, of a class extending Runnable.

If I am going wrong? Where am i missing? Or what's the way to start a dead thread?

clarifications are highly appreciated.

Cheers,
Swamy
 
author and iconoclast
Posts: 24207
46
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

Originally posted by Ramaswamy Srinivasan:
Or what's the way to start a dead thread?



You can't. Once a Thread has been started() and its run() method has returned, the Thread object is "dead" and it cannot be reused; you have to create a new one.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you must have some code that runs on a thread and can be run more than once, you can create a new Thread with an existing Runnable.

Or you can do some cool stuff with making the Thread wait instead of ending. Thread pools do this and there have been neat posts in the past about doing it outside of a pool. This shows how a thread pool might wait for the next available command to execute and you could send the same command twice:

If either of those approaches sound useful we can get into more details.
 
reply
    Bookmark Topic Watch Topic
  • New Topic