• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding the main and System.exit

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i looped on the Thread.getAllStackTraces(). i am getting the following output:



But what does this mean?
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter Chase for your immense help i solved the problem.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic