PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.2.0-rc0/hive/external/common/src/java/org/apache/hadoop/hive/common/HiveInterruptUtils.java

#
Java | 70 lines | 36 code | 8 blank | 26 comment | 2 complexity | f0e1a48e1c0dd8e9e544fbf33487022e MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.common;
20
21import java.util.ArrayList;
22import java.util.List;
23
24public class HiveInterruptUtils {
25
26  /**
27   * A list of currently running comments that needs cleanup when the command is canceled
28   */
29  private static List<HiveInterruptCallback> interruptCallbacks = new ArrayList<HiveInterruptCallback>();
30
31  public static HiveInterruptCallback add(HiveInterruptCallback command) {
32    synchronized (interruptCallbacks) {
33      interruptCallbacks.add(command);
34    }
35    return command;
36  }
37
38  public static HiveInterruptCallback remove(HiveInterruptCallback command) {
39    synchronized (interruptCallbacks) {
40      interruptCallbacks.remove(command);
41    }
42    return command;
43  }
44
45  /**
46   * Request interruption of current hive command
47   */
48  public static void interrupt() {
49    synchronized (interruptCallbacks) {
50      for (HiveInterruptCallback resource : new ArrayList<HiveInterruptCallback>(interruptCallbacks)) {
51        resource.interrupt();
52      }
53    }
54  }
55
56  /**
57   * Checks if the current thread has been interrupted and throws RuntimeException is it has.
58   */
59  public static void checkInterrupted() {
60    if (Thread.currentThread().isInterrupted()) {
61      InterruptedException interrupt = null;
62      try {
63        Thread.sleep(0);
64      } catch (InterruptedException e) {
65        interrupt = e;
66      }
67      throw new RuntimeException("Interuppted", interrupt);
68    }
69  }
70}