Reputation: 5098
I have a swing application with an exit button that calls a method which invokes System.exit(0); but the JVM doesn't shut down for some reason. Does anyone know what could cause the JVM to continue running?
Upvotes: 4
Views: 3046
Reputation: 805
Non-daemon threads will not keep the program from terminating when you call System.exit()
(and daemon threads certainly won't) but shutdown hooks† will, if they fail to terminate.
† See Design of the Shutdown Hooks API (the originally linked article is now only available at the wayback machine).
Swing probably uses shutdown hooks, but not in a way that would prevent your program from terminating. But if you (or some library you use) call Runtime.addShutdownHook()
, you'd better make sure your shutdown hooks don't deadlock.
By the way, one work-around for this problem is to make add a shutdown hook that spins off another thread that waits for sixty seconds, then calls Runtime.halt()
. (You have to spin off another thread because otherwise the shutdown hook, by waiting sixty seconds, will prevent the program from terminating for sixty seconds.)
Upvotes: 5
Reputation: 918
Maybe you have a Thread that is still running?
It's quite easy to find which threads are running and when. In Netbeans, start your project in Profile mode (Alt+F2) to have a snapshot in real-time. (Profiler -> View ->Threads)
Upvotes: 2