java - Maintaining a single executor service and shutting it down appropriately -


my application requires job done asynchronously, i've resorted executor framework. job (a runnable) expected run should external interface down (the vendor backed while, say, 30 mins).

with ask in mind, believe should maintain single scheduled executor service fixed no. of core threads perform these jobs when interface said above goes down brief moment (as static variable in class need thread pool in). don't think should create executor every need handle such job (single scheduled thread pool executor), , shut down after job run, because defeat purpose, wouldn't it? because mean reserving thread every job during backed hour sounds scary me.

but, if maintain such single executor service, when shutting down executor? understand executor once shut down can't reused, while not shutting down executor , keeping threads active prevent jvm shutting down. how go this?

my last statement based on effective java 2nd edition:

and here how tell executor terminate gracefully (if fail this, vm not exit):

executor.shutdown();

this job (a runnable) expected run should external interface down (the vendor backed while, say, 30 mins).

above requirement. solution entirely depends on how above situation handled.

  • trigger or event listener: if have trigger or event listener can called when found external interface down, in triggered code or event listener, can create executorservice, execute tasks (you can choose on fixed or pooled thread pool), , on completion of tasks, can shutdown executorservice
    in case, idea create executorservice, tasks , shut down. no need of long running executorservice.

  • track or periodic check: if have track or check periodically whether external interface down or not, think can have scheduledthreadpoolexecutor implementation have check after fixed time interval whether external interface down or not, , if down execute tasks. in case not shutdown executorservice , running.

p.s.: trigger , track own convention used tangentially 1 word, infer in technical words.

i understand executor once shut down can't reused, while not shutting down executor , keeping threads active prevent jvm shutting down.

yes, executor once shut down can't reused.
no, running executorservice not stop jvm shutdown, once jvm shutdown executorservice instance , thread pool stopped , destroyed.


Comments