wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes regarding the main and System.exit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regarding the main and System.exit" Watch "regarding the main and System.exit" New topic
Author

regarding the main and System.exit

subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Hi All,

I am running one application program, what it has do is:

1) Open input file read it and insert the records in the database
2) So i am using BufferedReader, BufferedWritter and Jdbc
3) I am closing all the resources in the finally block (all checks are done for null...)

Problem is:
The main never exit by itself

Means if i put the
line at the end of the main all is doing well otherwise program doesn't terminate.

Has anybody faced this problem?


http://subodh-gupta.blogspot.com
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Get yourself an all-threads dump, when the application is supposed to have finished, and see what the threads are doing. To get an all-threads dump you can press CTRL-BREAK in a Windows command prompt, or send SIGQUIT (I think) on Unix. Or you can do it programmatically, with Thread.getAllStackTraces().

If the all-threads dump does not make sense to you, you can post it here and someone will help you out.

If you are using Swing, be aware that special measures (not too difficult) are needed to ensure a Swing application exits, without using System.exit().

If you are using JDBC, your database may have started some threads. You may need to do something to shut it down.

Avoiding System.exit() is a very worthy cause, as using System.exit() is a Bad Thing, except in throw-away code; proper applications should never System.exit() IMHO.
[ September 27, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Does your program start any threads? If there are multiple threads running, the program will not stop at the end of the main() method; it will stop when all threads stop.

If your program itself doesn't start new threads, then it's possible that something else, for example the JDBC driver that you're using, is doing it.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
when i looped on the Thread.getAllStackTraces(). i am getting the following output:



But what does this mean?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I'm not sure exactly what you've printed out here. The all-threads dump obtained by Ctrl-Break/SigQUIT is more easily understood. Can you either get that or explain exactly how you obtained the print-out you've already posted?

One thing we need to know is which threads are daemon and which are non-daemon. The Ctrl-Break/SigQUIT print-out will include that information. If you want to continue using programmatic method instead, you'll need to print out that information, for each thread.
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
I obtain it like this one



I know i have printed the object.toString() method but i dont know what you need out of this .
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It would help if there was a gap after each thread (the outer loop). But the main thing needed is, like I said, information about whether each thread is daemon or not.

The keys of the map are Thread objects. I suggest you print out the name and daemon status of each Thread, above its StackTraceElements.

Just doing toString() on each StackTraceElement is fine, for those.
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
The code:



The result:

CleanerThread_4 --> false
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Unknown Source)
snaq.util.ObjectPool$Cleaner.run(ObjectPool.java:823)
main --> false
java.lang.Thread.dumpThreads(Native Method)
java.lang.Thread.getAllStackTraces(Unknown Source)
com.shinseisec.hudson.cm.console.DailyMTMBatch.main(DailyMTMBatch.java:168)
Finalizer --> true
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
CleanerThread_1 --> false
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Unknown Source)
snaq.util.ObjectPool$Cleaner.run(ObjectPool.java:823)
CleanerThread_2 --> false
java.lang.Thread.sleep(Native Method)
snaq.util.ObjectPool$Cleaner.run(ObjectPool.java:829)
CleanerThread_3 --> false
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Unknown Source)
snaq.util.ObjectPool$Cleaner.run(ObjectPool.java:823)
Signal Dispatcher --> true
Reference Handler --> true
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Unknown Source)
java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
CleanerThread_0 --> false
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Unknown Source)
snaq.util.ObjectPool$Cleaner.run(ObjectPool.java:823)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
These look like your perps: -



You have several threads doing snaq.util.ObjectPool$Cleaner.run(). These threads are non-daemon (as evidenced by the "false"), so the JVM will not exit until they are finished. But they seem to be waiting for some notification that is probably never going to come.

The name ObjectPool suggests these are some sort of pool (of database connections?). If these ObjectPool and Cleaner objects have a shut-down protocol, perhaps you are not calling it, and you need to do so. This might not be a direct call. For instance, perhaps you need to call some method associated with your JDBC driver, and then that would shut down the ObjectPool and Cleaner objects.

Alternatively, do you have any control over how the threads for these Cleaners are launched? If you do, you could set them to be daemon threads. Then the JVM would ignore them, when deciding whether or not to shut down. A thread can only be set to daemon before it's started, so you can't change running threads to daemon.
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Hi,

I am using the connection pool which is for the web application also and i am just taking the connection from their because my application will be scheduled to fill the data in that application's database (like a batch run on a scheduler) and that pool will be their even when my application goes so what should be done?
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Thanks Peter Chase for your immense help i solved the problem.
 
 
subject: regarding the main and System.exit
 
Similar Threads
main problem
main thread not exiting
System.exit(0);
Switch Statement
can a program without "main" method show output?