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.hbase.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    @Override
49    public abstract void run();
50    
51    //// Begin delegation to Thread
52    
53    public final String getName() {
54      return thread.getName();
55    }
56  
57    public void interrupt() {
58      thread.interrupt();
59    }
60  
61    public final boolean isAlive() {
62      return thread.isAlive();
63    }
64  
65    public boolean isInterrupted() {
66      return thread.isInterrupted();
67    }
68  
69    public final void setDaemon(boolean on) {
70      thread.setDaemon(on);
71    }
72  
73    public final void setName(String name) {
74      thread.setName(name);
75    }
76  
77    public final void setPriority(int newPriority) {
78      thread.setPriority(newPriority);
79    }
80  
81    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
82      thread.setUncaughtExceptionHandler(eh);
83    }
84  
85    public void start() {
86      thread.start();
87    }
88    
89    public final void join() throws InterruptedException {
90      thread.join();
91    }
92  
93    public final void join(long millis, int nanos) throws InterruptedException {
94      thread.join(millis, nanos);
95    }
96  
97    public final void join(long millis) throws InterruptedException {
98      thread.join(millis);
99    }
100   //// End delegation to Thread
101 }