ExecutorService is not shutting down

前端 未结 5 503
独厮守ぢ
独厮守ぢ 2021-01-13 06:17

I have the following code

    ExecutorService es = Executors.newSingleThreadExecutor();
    es.submit(new Runnable() {
           @Override public void run()         


        
5条回答
  •  耶瑟儿~
    2021-01-13 07:02

    isInterrupted() returns true if and only if thread's execution is interrupted.

         ExecutorService es = Executors.newSingleThreadExecutor();
            es.submit(new Runnable() {
                   @Override public void run() 
                   {   // infinite loop to process
    
                           while(true)
                           {                              
                              // We've been interrupted: no more processing.
                              if(Thread.currentThread().isInterrupted()){
                                 return;
                               }
                           }
    
                    }
           });
       es.shutdownNow();
    

提交回复
热议问题