com.cedarsoft.concurrent.ThreadDeadlockDetector Maven / Gradle / Ivy
/**
* Copyright (C) cedarsoft GmbH.
*
* Licensed under the GNU General Public License version 3 (the "License")
* with Classpath Exception; you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.cedarsoft.org/gpl3ce
* (GPL 3 with Classpath Exception)
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation. cedarsoft GmbH designates this
* particular file as subject to the "Classpath" exception as provided
* by cedarsoft GmbH in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
* or visit www.cedarsoft.com if you need additional information or
* have any questions.
*/
package com.cedarsoft.concurrent;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.annotation.Nonnull;
import com.cedarsoft.unit.si.ms;
/**
* ThreadDeadlockDetector class.
*
* @author Johannes Schneider ([email protected])
*/
public class ThreadDeadlockDetector {
/**
* The number of milliseconds between checking for deadlocks.
* It may be expensive to check for deadlocks, and it is not
* critical to know so quickly.
*/
@ms
private static final long DEFAULT_DEADLOCK_CHECK_PERIOD = 10_000;
@Nonnull
private final Timer threadCheck = new Timer( "ThreadDeadlockDetector", true );
@Nonnull
private final ThreadMXBean mXBean = ManagementFactory.getThreadMXBean();
private final long deadlockCheckPeriod;
@Nonnull
private final Collection listeners = new CopyOnWriteArraySet<>();
/**
* Constructor for ThreadDeadlockDetector.
*/
public ThreadDeadlockDetector() {
this( DEFAULT_DEADLOCK_CHECK_PERIOD );
}
/**
* Constructor for ThreadDeadlockDetector.
*
* @param deadlockCheckPeriod a int.
*/
public ThreadDeadlockDetector( long deadlockCheckPeriod ) {
this.deadlockCheckPeriod = deadlockCheckPeriod;
}
public void start() {
threadCheck.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
checkForDeadlocks();
}
}, this.deadlockCheckPeriod, this.deadlockCheckPeriod);
}
public void stop() {
threadCheck.cancel();
}
private void checkForDeadlocks() {
long[] ids = findDeadlockedThreads();
if ( ids != null && ids.length > 0 ) {
Set threads = new HashSet();
for ( long id : ids ) {
threads.add( findMatchingThread( mXBean.getThreadInfo( id ) ) );
}
fireDeadlockDetected( threads );
}
}
private long[] findDeadlockedThreads() {
// JDK 1.5 only supports the findMonitorDeadlockedThreads()
// method, so you need to comment out the following three lines
if ( mXBean.isSynchronizerUsageSupported() ) {
return mXBean.findDeadlockedThreads();
} else {
return mXBean.findMonitorDeadlockedThreads();
}
}
private void fireDeadlockDetected( @Nonnull Set extends Thread> threads ) {
for ( Listener listener : listeners ) {
listener.deadlockDetected( threads );
}
}
@Nonnull
private static Thread findMatchingThread( @Nonnull ThreadInfo threadInfo ) {
for ( Thread thread : Thread.getAllStackTraces().keySet() ) {
if ( thread.getId() == threadInfo.getThreadId() ) {
return thread;
}
}
throw new IllegalStateException( "Deadlocked Thread not found" );
}
/**
* addListener
*
* @param listener a ThreadDeadlockDetector.Listener object.
* @return a boolean.
*/
public boolean addListener( @Nonnull Listener listener ) {
return listeners.add( listener );
}
/**
* removeListener
*
* @param listener a ThreadDeadlockDetector.Listener object.
* @return a boolean.
*/
public boolean removeListener( @Nonnull Listener listener ) {
return listeners.remove( listener );
}
/**
* This is called whenever a problem with threads is detected.
*/
@FunctionalInterface
public interface Listener {
void deadlockDetected( @Nonnull Set extends Thread> deadlockedThreads );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy