Why does the ExecutorService interface not implement AutoCloseable?

前端 未结 3 1754
北恋
北恋 2020-12-29 04:01

Failing to call shutdown() on a thread executor will result in a never terminating application.

Best practice to shut down the ExecutorService is this:

3条回答
  •  旧时难觅i
    2020-12-29 04:52

    That ExecutorService has actually two shutdown-related methods; based on the simple fact that both ways of shutting down a service make sense.

    Thus: how would you auto-close a service then? In a consistent manner that works for everybody?!

    So, the reasonable explanation in my eyes: you can't make an ExecutorService a AutoClosable because that service does not have a single "close" like operation; but two!

    And if you think you could make good use of such an auto-closing service, writing up your own implementation using "delegation" would be a 5 minute thing! Or probably 10 minutes, because you would create one version calling shutdown() as close operation; and one that does shutdownNow() instead.

提交回复
热议问题