View Javadoc

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  package org.apache.hadoop.hbase.util;
19  
20  import org.apache.hadoop.classification.InterfaceAudience;
21  
22  import java.lang.Thread.UncaughtExceptionHandler;
23  
24  /**
25   * Abstract class which contains a Thread and delegates the common Thread
26   * methods to that instance.
27   * 
28   * The purpose of this class is to workaround Sun JVM bug #6915621, in which
29   * something internal to the JDK uses Thread.currentThread() as a monitor
30   * lock. This can produce deadlocks like HBASE-4367, HBASE-4101, etc.
31   */
32  @InterfaceAudience.Private
33  public abstract class HasThread implements Runnable {
34    private final Thread thread;
35    
36    public HasThread() {
37      this.thread = new Thread(this);
38    }
39  
40    public HasThread(String name) {
41      this.thread = new Thread(this, name);
42    }
43    
44    public Thread getThread() {
45      return thread;
46    }
47    
48    public abstract void run();
49    
50    //// Begin delegation to Thread
51    
52    public final String getName() {
53      return thread.getName();
54    }
55  
56    public void interrupt() {
57      thread.interrupt();
58    }
59  
60    public final boolean isAlive() {
61      return thread.isAlive();
62    }
63  
64    public boolean isInterrupted() {
65      return thread.isInterrupted();
66    }
67  
68    public final void setDaemon(boolean on) {
69      thread.setDaemon(on);
70    }
71  
72    public final void setName(String name) {
73      thread.setName(name);
74    }
75  
76    public final void setPriority(int newPriority) {
77      thread.setPriority(newPriority);
78    }
79  
80    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
81      thread.setUncaughtExceptionHandler(eh);
82    }
83  
84    public void start() {
85      thread.start();
86    }
87    
88    public final void join() throws InterruptedException {
89      thread.join();
90    }
91  
92    public final void join(long millis, int nanos) throws InterruptedException {
93      thread.join(millis, nanos);
94    }
95  
96    public final void join(long millis) throws InterruptedException {
97      thread.join(millis);
98    }
99    //// End delegation to Thread
100 }