Graceful shutdown of threads and executor

前端 未结 5 1507
一个人的身影
一个人的身影 2020-11-29 18:42

The following piece of code tries to accompolish this.

The code loops forever and checks if there are any pending requests to be processed. If there is any, it creat

5条回答
  •  北海茫月
    2020-11-29 19:21

    You can either call shutdown() on the ExecutorService:

    Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

    or you can call shutdownNow():

    Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

    There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.

    Which one you call depends how badly you want it to stop....

提交回复
热议问题