Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Thread ques

 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question from Jqplus

It should error out with exception as we are changing the status of the thread.
But it prints for sometimes and ends.I don't know its showing ;) but It is a infinite loop here
[ June 11, 2002: Message edited by: swati gupta ]
[ June 11, 2002: Message edited by: swati gupta ]
[ June 11, 2002: Message edited by: swati gupta ]
Edited by Corey McGlone: Turned off smilies to make the code more readable.
[ June 11, 2002: Message edited by: Corey McGlone ]
 
Charlie Sturman
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if isAlive()
setDaemon() throws IllegalThreadStateException()
isAlive()
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
public final native boolean isAlive();
end of trail
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why we still see some output after the exception is thrown?
Francisco
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1097)running..
running...
running...
running...
running...
running...
running...
running...
at Test.main(Test.java:20)
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
running...
[ June 12, 2002: Message edited by: Francisco A Guimaraes ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess because the deamon thread is still running for a while : for the lapse the JVM needs to check that no user thread is still running.
 
Robert Alkire
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyThread extends Thread
{
static int i;
public void run() {
for(; {
System.out.println("running..."+ ++i);
}
}
}
public class MTest{
public static void main(String args[]) throws Exception
{
MyThread mt = new MyThread();
mt.setDaemon(true);
mt.start();
// runs longer when mt.setDaemon(false); is "commented in"
// only because it takes time to create and throw an exception
mt.setDaemon(false);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic