Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Shutdown hook  RSS feed

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The shutdown hook executes but the program deosn not exit after, even though I have included the exit()
method.


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the code you showed has anything to do with why the application isn't shutting down. You probably have one of the following:
1) A thread in an infinite loop
2) A thread in a wait() that never gets notified
3) A thread pool (ExecutorService) which was never shut down
4) A deadlock situation between multiple threads

There are probably a bunch of other things that could be happening, but these are the most likely. You will need a profiler to figure out what is happening - which threads are still running and where in code they are.

A few comments about your shutdown hook though:
1) I would try to avoid GUI components in the shutdown hook. You are relying on the EDT, and the Swing components to be up and active to show your error messages. You would be much better off logging to a log file. In some version of Java (forget which) there was a bug which would cause javaw.exe to hang on exit if Swing components were used in a shutdown hook. I think these things were fixed late in 1.4 or early 1.5, but not sure. Still, the safest thing is to log to a file.

2) Do not call System.exit() from the shutdown hook. The shutdown hook is only running because the app is exiting. At best the System.exit() call is redundant. At worst it could cause problems or hide problems.

3) I don't really like your use of the try{} catch {} finally{} blocks. The code in the finally block can only be executed if the code in the try block succeeds. I think the better solution would be to put the success-dependent code inside the try block, rather than in the finally block with an if.

4) You should close the FileInputStream and FileOutputStream used for storing the properties.



 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for your insight Steve. Without the shutdown hook, the application exists completely. So as you explained it could have something to do with the properties file which is what I suspected. I will try your suggestions.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Steve, I tried your method. The program exits alright, but the column widths do no get properly recorded in the the properties file. I will try something else.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somehow your loop doesn't record the last column widths before shutdown. When I put back that long awkward sequence of code to do this, the colum widths were recorded properly and the program exited correctly with the series of catch phrases you supplied for which I am very grateful. This is exactly what I has wished for. Thank you once again for your effort and your help Steve.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the last column width wasn't recorded comes from the for loop - I went rather quick and was off by one:

Works for i from 0 to 4 (i <5). The highest value of i should be 'the number of columns.' You want to include column 5 so you would change that part of the code to:
Because there are 6 columns total (0-5).

You could probably make it a bit more robust (not having to hard code the number of columns here) by using getColumnCount() (assuming this.cm is a TableColumnModel):



Now for the 'fix' for hanging. Did you put back the JOptionPane for showing exception messages? I am curious to see if the problem was the use of Swing components in the Shutdown Hook showing up again. For posterity can you post what version of Java you are using to run the application, and if you are using WebStart to run it?

Steve
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!