001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import java.lang.Thread.UncaughtExceptionHandler;
021
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Abstract class which contains a Thread and delegates the common Thread
026 * methods to that instance.
027 * 
028 * The purpose of this class is to workaround Sun JVM bug #6915621, in which
029 * something internal to the JDK uses Thread.currentThread() as a monitor
030 * lock. This can produce deadlocks like HBASE-4367, HBASE-4101, etc.
031 */
032@InterfaceAudience.Private
033public abstract class HasThread implements Runnable {
034  private final Thread thread;
035  
036  public HasThread() {
037    this.thread = new Thread(this);
038    this.thread.setDaemon(true);
039  }
040
041  public HasThread(String name) {
042    this.thread = new Thread(this, name);
043    this.thread.setDaemon(true);
044  }
045  
046  public Thread getThread() {
047    return thread;
048  }
049
050  @Override
051  public abstract void run();
052  
053  //// Begin delegation to Thread
054  
055  public final String getName() {
056    return thread.getName();
057  }
058
059  public void interrupt() {
060    thread.interrupt();
061  }
062
063  public final boolean isAlive() {
064    return thread.isAlive();
065  }
066
067  public boolean isInterrupted() {
068    return thread.isInterrupted();
069  }
070
071  public final void setDaemon(boolean on) {
072    thread.setDaemon(on);
073  }
074
075  public final void setName(String name) {
076    thread.setName(name);
077  }
078
079  public final void setPriority(int newPriority) {
080    thread.setPriority(newPriority);
081  }
082
083  public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
084    thread.setUncaughtExceptionHandler(eh);
085  }
086
087  public void start() {
088    thread.start();
089  }
090  
091  public final void join() throws InterruptedException {
092    thread.join();
093  }
094
095  public final void join(long millis, int nanos) throws InterruptedException {
096    thread.join(millis, nanos);
097  }
098
099  public final void join(long millis) throws InterruptedException {
100    thread.join(millis);
101  }
102  //// End delegation to Thread
103}