jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Thread ques Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Thread ques" Watch "Another Thread ques" New topic
Author

Another Thread ques

Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
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

Joined: Apr 04, 2002
Posts: 112
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
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

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
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
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);
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Another Thread ques
 
Similar Threads
Thread Ques
Thread
Thread Ques
Try this question I made up.
all setDaemon() after the thread is started