Reputation: 3533
I am working on this application that requires loading of native library. the loading of the class that initiates the loading of the native library is running on a different thread from the main thread. When an error occurs (an error is thrown), i would expect that the vm terminates, but in this case it does not. certainly when i cant load the native library, i must not continue, because thats the core of the application. I assume my application is not terminating because the main thread starts at least 3 threads. the connection thread-that listens if a gadget has been connected and then informs the listener that it has been connected or disconnected, and other resource management threads.
How would i end my application when the jls says that the application should not try to catch errors?
Upvotes: 0
Views: 187
Reputation: 1538
In order to answer your question properly, I'll need to know more information about the other threads that you spawned. Can they be set to daemon-type threads? If not, how do you normally gracefully exit those threads?
Here's a brute-force way (inside the thread that loads the native library):
try {
methodToLoadLibrary();
doWhatYouNeedToDo();
} finally {
System.exit(1);
}
but this assumes that you'll be exiting the entire process when this thread terminates.
If you have a method which can be called to make the threads terminate gracefully, you can replace the System.exit(1);
call with this:
Collection<Stoppable> sList;
for(Stoppable s : sList) {
s.stop();
}
where Stoppable
is the interface for communicating that you want a graceful shutdown on those threads.
There are also less-clean ways like calling Thread.stop
on the other threads.
Upvotes: 1