Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Shutdown Hook question.

Ranch Hand
Posts: 268
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application which I launch from the command line (MacOS). I have added a shutdown hook to my initial code so that it closes down gracefully.

The entry point for the application is non visual and runs in a terminal window. If I close terminal by either using the (red) button on the terminal window - or use the pull down menu for the active application title bar then my shutdown hook code is triggered and the application shuts down cleanly.

So - now for the tricky bit to explain. My terminal window which is running creates a number of JFrames which I display real time information in. If one of these windows has focus then the Mac title bar changes from showing Terminal to the name of the initial Class name of my GUI. I suppose that's reasonable.

However if I now use that new title bar at the top of MacOS and select Quit for the GUI component then my shutdown hook does not get called and in fact I have to forcibly terminate the application.

I doubt it's the case but it almost appears that the startup code running in the terminal window is using one JVM and the GUI code is running another JVM - the later not having a shutdown hook.

I appreciate this probably not a good explanation and might be mac specific.

I have added 2 screen dumps.

Image 1

Image 2

Saloon Keeper
Posts: 22634
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid your screen dumps didn't make the trip.

Although parts of what you're describing sound like how the original MacOS handled title bars - which differed from other GUIs - I think your real problem is not really MacOS related.

A Java application does not truly terminate - that is, the JVM does not exit - until all threads in the JVM have terminated. As long as even one thread lives, the JVM shutdown will hang.

There are quite a few threads running in your average JVM, but most of them (stuff like the garbage collector) understand when shutdown has been signalled and they then terminate themselves. That includes, in the case of a GUI system like Swing, the rendering and UI threads.

So killing just one window by brute force might not be enough. Ideally you should have an "Exit" command action (tied to things like the File/Exit menu) that will initiate a clean shutdown, but if you should kill processes relating to just one window, for example, then something like what you seem to be describing would apply - the remaining threads would hang the JVM shutdown process.

Note that doing an OS-level termination ("Force close/quit" for desktops that support it or a "kill" from command line) terminate the JVM by raw brute force, and in such cases no hooks would be able to run. However if you can trap the OS signal (for example SIGKILL) then you can fire the "Exit" command action and (we hope) shut down properly.
David Garratt
Ranch Hand
Posts: 268
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did notice my images did not make it so I have provided some links instead. Might help.

Posts: 22015
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should checkout:
* The default close operation for JFrame and JDialog.
* WindowListeners.

With both you can define what happens if you click on the (red) close icon of the window (JFrame, JDialog). The default close operation is HIDE_ON_CLOSE, which means that the JFrame/JDialog will no longer be visible, but it still holds resources. I prefer DISPOSE_ON_CLOSE (not EXIT_ON_CLOSE), as it allows the Event Dispatcher Thread to shutdown correctly once all windows are disposed. EXIT_ON_CLOSE calls System.exit, which terminates all threads immediately.
What's that smell? I think this tiny ad may have stepped in something.
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic