• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic