This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does program terminate when main thread terminates ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Does program terminate when main thread terminates ?" Watch "Does program terminate when main thread terminates ?" New topic
Author

Does program terminate when main thread terminates ?

giang nguyen
Ranch Hand

Joined: May 13, 2003
Posts: 42
Dear all,
I've seen the anwser for the above question is true. But I myself think that if there're non-deamon threads running in main, the program still hasn't terminated yet, it keeps running untill all non-deamon threads finished.
My question is: Can main thread finish executing before all other non-deamon threads finish their jobs or the main thread always is the last thing to exist?
Your thought ?


SCJP 1.4, SCWCD
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Can main thread finish executing before all other non-daemon threads finish their jobs

Yes
or the main thread always is the last thing to exist?

No
We can use the isAlive() method to show that Thread-1 continues to execute after the main thread has died.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
According to The Java Programming Language, when the last user thread has finished, any daemon threads are terminated and the application is finished. This termination is like that of destroy�abrupt and with no chance of cleanup�so daemon threads are limited in what they can do.
If we start a new thread as a daemon, when the main thread dies, the daemon thread runs for a little while, but does not complete.

I use to think the daemon threads aborted immediately. But a few experiments show they hang around for not more than a millisecond before aborting.
[ October 28, 2003: Message edited by: Marlene Miller ]
giang nguyen
Ranch Hand

Joined: May 13, 2003
Posts: 42
Thanks for your professional answer, Marlene Miller. The code examples are very good and help me to understand the problem. I am going to take the SCJP exam next week and want to clear as much as possible doubt in my mind. I ask this question because one of the answer in the mock exam that I am using saying that the main thread must be the last thread to finish execution, when main thread stops the program terminates. This is contrary to what I've known so far.
Thanks again.
Vinod Sinha
Ranch Hand

Joined: Oct 16, 2003
Posts: 43
Consider the undermentioned sources :

1. Java Language Specification :
Section 12.8 Program Exit
A program terminates all its activity and exits when one of two things happens:
• All the threads that are not daemon threads terminate.
• Some thread invokes the exit method of class Runtime or class System and
the exit operation is not forbidden by the security manager.

------------------------------------------------------------------
2. Introduction to Java threads
ibm.com/developerWorks
quote
"We mentioned that a Java program exits when all of its threads have completed, but this is not
exactly correct. What about the hidden system threads, such as the garbage collection thread
and others created by the JVM? We have no way of stopping these. If those threads are
running, how does any Java program ever exit?
These system threads are called daemon threads. A Java program actually exits when all its
non-daemon threads have completed."
unquote

Therefore , Daemon threads are not forcefully terminated as mentioned
by you.
Correct me if I am wrong in the interpretation of the sources
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Vinod,
I guess we will never know for sure what happens to those daemon threads unless we can read the source code of the virtual machine. Meanwhile, we each have to decide who will be our thread teacher.
My thread teacher is Doug Lea, Concurrent Programming in Java. This is what he says.
The method setDaemon asserts that the JVM may exit, abruptly terminating the thread, so long as all other non-daemon threads in the program have terminated. CPJ 1.1.2.1
From some code experiments like the ones above, I understand better that the JVM may exit (not will exit). My tests show that the daemon thread does not complete the task in the run method.
The Java Programming Language has a section on Shutdown 18.3. I haven�t read that very carefully.
[ October 29, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is a nice simple test. The run method has an infinite loop. Since the new thread is a daemon, it terminates.

[ October 29, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Giang. Thank you for your response. It help me to know if what I have said makes sense and is helpful.
I am sorry to keeping posting more stuff. But I am learning also. So I keep thinking about this. This idea just popped into my head.
When you write a Java GUI program, your main method might create a JFrame and a bunch of JPanels, JButtons etc. Then you set visible to true. Then the main thread completes, but the program continues running. The Swing components are being managed by two AWT user threads. This is an good example of the main thread dying and the other threads continuing to exist.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does program terminate when main thread terminates ?
 
Similar Threads
How setDaemon works
Daemon and non deomon threads?
thread
how to generate a deamon thread in java
Another JQ+ Thread