Biscuit128
Biscuit128

Reputation: 5398

When should application cleanup occur

I was just reading that we can use shutdown hooks as an example of a time when it would be ideal to provide functions to clean up before application exit.

What I was wondering is, if for example you have an application that is to run 24/7 like a web service, when would an ideal time be to terminate and cleanup things such as Executor Services and so on. If the application is not supposed to be down then really we don't have a convenient clean up period or indeed the ability to shutdown cleanly?

Thanks

Upvotes: 0

Views: 39

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172518

The question is too broad but if I understood it correct then the Oracle docs explain it:

A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

So for example ExecutorService which is created by Executors.newSingleThreadExecutor() is an instance of FinalizableDelegatedExecutorService. Now the ExecutorService class has finalize() method that calls shutdown() method on the wrapped ExecutorService object.

Upvotes: 1

Related Questions