View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.util.internal;
17  
18  import java.util.concurrent.Executor;
19  import java.util.concurrent.ExecutorService;
20  import java.util.concurrent.TimeUnit;
21  
22  /**
23   * Shuts down a list of {@link Executor}s.  {@link #terminate(Executor...)} will
24   * shut down all specified {@link ExecutorService}s immediately and wait for
25   * their termination.  An {@link Executor} which is not an {@link ExecutorService}
26   * will be ignored silently.
27   *
28   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
29   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
30   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
31   */
32  public class ExecutorUtil {
33  
34      /**
35       * Returns {@code true} if and only if the specified {@code executor}
36       * is an {@link ExecutorService} and is shut down.  Please note that this
37       * method returns {@code false} if the specified {@code executor} is not an
38       * {@link ExecutorService}.
39       */
40      public static boolean isShutdown(Executor executor) {
41          if (executor instanceof ExecutorService) {
42              if (((ExecutorService) executor).isShutdown()) {
43                  return true;
44              }
45          }
46          return false;
47      }
48  
49      /**
50       * Shuts down the specified executors.
51       */
52      public static void terminate(Executor... executors) {
53          // Check nulls.
54          if (executors == null) {
55              throw new NullPointerException("executors");
56          }
57  
58          Executor[] executorsCopy = new Executor[executors.length];
59          for (int i = 0; i < executors.length; i ++) {
60              if (executors[i] == null) {
61                  throw new NullPointerException("executors[" + i + "]");
62              }
63              executorsCopy[i] = executors[i];
64          }
65  
66          // Check dead lock.
67          final Executor currentParent = DeadLockProofWorker.PARENT.get();
68          if (currentParent != null) {
69              for (Executor e: executorsCopy) {
70                  if (e == currentParent) {
71                      throw new IllegalStateException(
72                              "An Executor cannot be shut down from the thread " +
73                              "acquired from itself.  Please make sure you are " +
74                              "not calling releaseExternalResources() from an " +
75                              "I/O worker thread.");
76                  }
77              }
78          }
79  
80          // Shut down all executors.
81          boolean interrupted = false;
82          for (Executor e: executorsCopy) {
83              if (!(e instanceof ExecutorService)) {
84                  continue;
85              }
86  
87              ExecutorService es = (ExecutorService) e;
88              for (;;) {
89                  try {
90                      es.shutdownNow();
91                  } catch (SecurityException ex) {
92                      // Running in a restricted environment - fall back.
93                      try {
94                          es.shutdown();
95                      } catch (SecurityException ex2) {
96                          // Running in a more restricted environment.
97                          // Can't shut down this executor - skip to the next.
98                          break;
99                      } catch (NullPointerException ex2) {
100                         // Some JDK throws NPE here, but shouldn't.
101                     }
102                 } catch (NullPointerException ex) {
103                     // Some JDK throws NPE here, but shouldn't.
104                 }
105 
106                 try {
107                     if (es.awaitTermination(100, TimeUnit.MILLISECONDS)) {
108                         break;
109                     }
110                 } catch (InterruptedException ex) {
111                     interrupted = true;
112                 }
113             }
114         }
115 
116         if (interrupted) {
117             Thread.currentThread().interrupt();
118         }
119     }
120 
121     private ExecutorUtil() {
122         super();
123     }
124 }