001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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 */
018
019package org.jppf.utils.concurrent;
020
021import org.jppf.utils.ExceptionUtils;
022import org.slf4j.*;
023
024/**
025 * A thread that prints out the call stack when {@link #interrupt()} is called.
026 */
027public class DebuggableThread extends Thread {
028  /**
029    * Logger for this class.
030    */
031  private static final Logger log = LoggerFactory.getLogger(DebuggableThread.class);
032  /**
033   * Determines whether the debug level is enabled in the log configuration, without the cost of a method call.
034   */
035  private final static boolean debugEnabled = log.isDebugEnabled();
036
037  /**
038   * Construct this thread.
039   * @param name the name of this thread.
040   */
041  public DebuggableThread(final String name) {
042    super(name);
043  }
044
045  /**
046   * Construct this thread.
047   * @param target the associate {@code Runnable}.
048   */
049  public DebuggableThread(final Runnable target) {
050    super(target);
051  }
052
053  /**
054   * Construct this thread.
055   * @param target the associate {@code Runnable}.
056   * @param name the name of this thread.
057   */
058  public DebuggableThread(final Runnable target, final String name) {
059    super(target, name);
060  }
061
062  /**
063   * Construct this thread.
064   * @param group the thread group owning this thread.
065   * @param target the associate {@code Runnable}.
066   * @param name the name of this thread.
067   */
068  public DebuggableThread(final ThreadGroup group, final Runnable target, final String name) {
069    super(group, target, name);
070  }
071
072  @Override
073  public void interrupt() {
074    if (debugEnabled) log.debug("interrupt() called on {} by {}, call stack:\n{}", this, Thread.currentThread(), ExceptionUtils.getCallStack());
075    super.interrupt();
076  }
077}