• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Patrick Naughton and Threads

 
Sanjeev Verma
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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...
 
mohit joshi
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic