jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Threads : Restarting a dead Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Threads : Restarting a dead Thread" Watch "Threads : Restarting a dead Thread" New topic

Threads : Restarting a dead Thread

kunal Jaggi

Joined: Sep 16, 2003
Posts: 17
We know that a dead thread can't be restarted although we can inspect its state (like if it is alive and priority etc..). A Thread is considered dead after the call to run() returns or calling dead() explicitly. Further restarting a dead thread throws an Exception. Then why the following code does not throw an Exception.
class MyThread extends Thread{
public void run(){
public class TestProgram{
public static void main(String args[]){
MyThread ref=new MyThread();
ref.start();//scheduling the thread
Thread.sleep(3000);//enough time for the thread to complete it's run() method
}catch(InterruptedException e){}
System.out.println("IsAlive-->"+ref.isAlive());//OK U can inspect the state, false
ref.start();//this should throw an exception???
Kunal Jaggi
[ September 16, 2003: Message edited by: kunalnimc Jaggi ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I added a second start() call immediately after the first, and that one sure enough gets an exception for attempting to start a thread that is already started. The docs make it sound like that's the fatal error, but don't tell you that calling start() on a thread that has run to completion with do nothing at all. Anybody know if that's the intention?

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
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Wow, that's really weird. Most disturbing is that it apparently does not "do nothing" -- it sets the "isAlive()" state back to "true"!
I found this defect report at the JDC -- it does seem to imply that someone's reported that calling start() again doesn't cause an exception. The report is from 2001, listed as open still.
[ September 16, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
kunal Jaggi

Joined: Sep 16, 2003
Posts: 17
If this works, then calling setDaemon(boolean_primitive_type) after starting the Thread should also work............
Can anybody further explain what's going on here......
Kunal Jaggi
[ September 17, 2003: Message edited by: kunalnimc Jaggi ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Threads : Restarting a dead Thread
It's not a secret anymore!