You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/07/19 14:03:27 UTC

[GitHub] panhongliang edited a comment on issue #510: dubbo的优雅关机真的优雅吗?

panhongliang edited a comment on issue #510: dubbo的优雅关机真的优雅吗?
URL: https://github.com/apache/incubator-dubbo/issues/510#issuecomment-321716227
 
 
   其实我想我说的是dubbo的优雅关机其实就是调用线程池的shutDownNow方法。但是如果你的代码写的不够好,shutDownNow是不能把你怎么样的。
   在ExecutorUtil类的优雅关机方法:
   
   ```java
    public static void gracefulShutdown(Executor executor, int timeout) {
           if (!(executor instanceof ExecutorService) || isShutdown(executor)) {
               return;
           }
           final ExecutorService es = (ExecutorService) executor;
           try {
               es.shutdown(); // Disable new tasks from being submitted
           } catch (SecurityException ex2) {
               return ;
           } catch (NullPointerException ex2) {
               return ;
           }
           try {
               if(! es.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {
                   es.shutdownNow();
               }
           } catch (InterruptedException ex) {
               es.shutdownNow();
               Thread.currentThread().interrupt();
           }
           if (!isShutdown(es)){
               newThreadToCloseExecutor(es);
           }
       }
   ```
   
   shutDownNow方法最终调用的线程的interrupt方法。interrupt方法只是给线程设置了一个中断标志位,然后还能打断sleep ,wait,join方法,使这些方方法抛异常,
   但是我们总不能随随便便就调用sleep,wait,join方法吧。
   所以我说如果你的代码写的不够好,shutDownNow方法对你是没有效果的。
   你可以这样:
   if(Thread.curentThread().isInterrupted()){
    // throw new Exception();然后就可以中断了。
   }
   
   可以参考下:
   http://blog.csdn.net/abountwinter/article/details/75277519

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org