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
Joined: Apr 04, 2002
Posts: 112
posted
0
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
Joined: Mar 20, 2002
Posts: 182
posted
0
but why we still see some output after the exception is thrown? Francisco
Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Francisco A Guimaraes
Ranch Hand
Joined: Mar 20, 2002
Posts: 182
posted
0
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
Joined: Jul 03, 2001
Posts: 2120
posted
0
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.
SCJP2. Please Indent your code using UBB Code
Robert Alkire
Greenhorn
Joined: May 13, 2002
Posts: 2
posted
0
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); } }