Executor and Daemon in Java

前端 未结 3 2027
北海茫月
北海茫月 2020-12-09 12:27

I have a MyThread object which I instantiate when my app is loaded through the server, I mark it as a Daemon thread and then call start() on it. The thread is m

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 12:46

    Just to complement with another possible solution for completeness. It may not be as nice though.

    final Executor executor = Executors.newSingleThreadExecutor();
    Runtime.getRuntime().addShutdownHook(new Thread() {
    
        @Override
        public void run() {
            executor.shutdownNow();
        }
    });
    

提交回复
热议问题