This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes 'start' and 'run' method - behaviors!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark " Watch " New topic
Author

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

Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
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 ]

Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
Don't get me started about those stupid light bulbs.
 
subject: 'start' and 'run' method - behaviors!!!
 
Similar Threads
thread problem
Threads...
one more thread
Thread & main()
thread start in k&b