Daemon threads scheduled on an ExecutorService; explain why this is bad form

后端 未结 3 1521
一整个雨季
一整个雨季 2021-02-01 06:18

I\'m comfortable with the idea of orderly shutdown on threads scheduled with an ExectuorService; that is to say, calling shutdown or shutdownNow<

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 06:46

    is it bad practice to use daemon threads in a ExecutorService's thread pool?

    If the tasks sent to that particular ExecutorService are ok to be terminated abruptly, then why not, that's what daemon threads do. But generally, there are not many tasks that are ok to be terminated with no shutdown ceremonies at all, so you must know what you're doing if you opt to daemon threads.

    finalize() is called when an object is about to be garbage collected. There are no guarantees on when, if ever, any particular object will be GCd, and ThreadPoolExecutor is no exception, so its finalize() may or may not be called. The behavior depends on the particular JRE implementation, and even with the same implementation, may vary from time to time.

提交回复
热议问题