简体   繁体   中英

Java - Force Close Program (with running threads)

In General
I want to be able to close my Java application even if there are (non-daemon) threads still running. Is it possible in Java? (Coming from C++ world, that seems like a basic request)

Reason
I'm building a framework that load external plugins that implement an interface. before shutting down I call a close() function on each plugin expect it to release all it resource and stop all his threads. But since the plugin are build by several teams at my workplace I can't really control them and more than once someone didn't close all his threads leaving the application stuck in the shutdown stage.
I would like to close the program forcefully after I called each plugin close() function, If they didn't take care of their resources it's their own problem...

I think instead of letting all the plugins create threads all over the place, its better to create a thread pool in your main program and give the plugins the possibility to submit Runnables and Callables to it. This will give you control over the concurrent tasks in your plugins, ie if there are 100 plugins, there dont have to be 100 threads.

Once you restart your program you can call shutdown on the pool, giving the plugins tasks time to tidy up resources. You can define a timeout also.

to exit the program try

 System.exit(0);

and try

 finalize();

if you want to release the resoureces

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM