When to shutdown executorservice in android application

前端 未结 2 1572
孤城傲影
孤城傲影 2021-02-13 14:09

Lets assume I\'m planning to use one executorservice in my entire application, to which I\'m sending new runnable\'s or callable\'s to execute/submit, and I ommit to shutdown ri

2条回答
  •  长发绾君心
    2021-02-13 14:41

    You can do one work around. Create the executor as daemon. Then it will automatically end when your application exits. You don't have to explicitly call the shutdown.

     ExecutorService es = Executors.newSingleThreadExecutor( new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });
    

提交回复
热议问题