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