• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads : Restarting a dead Thread

 
kunal Jaggi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(){
System.out.println("Running....");
}
}
public class TestProgram{
public static void main(String args[]){
MyThread ref=new MyThread();
ref.start();//scheduling the thread
try{
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???
System.out.println("IsAlive-->"+ref.isAlive());//true
}
}
Regards,
Kunal Jaggi
SCJP2
[ September 16, 2003: Message edited by: kunalnimc Jaggi ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
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
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 ]
 
kunal Jaggi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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......
Regards,
Kunal Jaggi
SCJP2
[ September 17, 2003: Message edited by: kunalnimc Jaggi ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic