0

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...

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94

2 Answers2

3

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, i.e. 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.

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • (+1) Thanks, we actually start using this approach a few days ago. but I can't prevent the plugins from keep creating their own threads... – Roee Gavirel Jan 07 '15 at 18:17
  • With a byte manipulation library you can! E.g. aspect oriented programming allow you to target a "new Thread()" which you then can change into throwing a runtime exception. You probably need to do this in agreement with all the teams. – Thorbjørn Ravn Andersen Nov 22 '15 at 12:16
1

to exit the program try

 System.exit(0);

and try

 finalize();

if you want to release the resoureces