Dear friends I came accross this in Patrick Naughtons' famous Java2: Complete Reference book (chap 11, page no. 277, second last line) "The main thread must be the last thread to finish execution. When the main thread stops, your program terminates." I have immense respect for the book as it is the starting point for almost all those who have interest in java. But in this case, every certification book says otherwise. Try this code:
The output is self explanatory. Or am I missing something ??? Please clarify Thanx Sanjeev [I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future. Learn more about UBB codes here - Ajith
[This message has been edited by Ajith Kallambella (edited September 27, 2000).]
Joined: Jan 30, 2000
"The main thread must be the last thread to finish execution. When the main thread stops, your program terminates." That is wrong if by "main thread" is meant the one started by the JVM to execute the <CODE>public static void main(String s)</CODE> method. This method may start other threads and then exit itself. When the last non-daemon thread ends, the JVM exits even if it has many daemon threads still alive. BTW, I didn't start my Java studies with that book. I don't even have it...
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Joined: Sep 23, 2000
I think what PN means is that if your program is running, and has some child threads started by the main thread, then if you destroy the main thread, i.e. when the main thread dies, then the other threads also die and the jvm exits. PN has used the term stop to mean that the thread is actually dead, and has not just finished executing. The difference between main thread and child threads is that, if the child thread starts a subchild thread, and then you destroy the child thread, the subchild thread keeps running. Hope this answers your question.