public static String getFormattedDeadlockInfo ( final String prefix , final String threadPrefix , final String stackElementPrefix ) { final StringBuilder message = new StringBuilder ( prefix ) ; final DeadlockInfo deadlockInfo = new DeadlockInfo ( ) ; final ThreadInfo [ ] deadlockedThreads = deadlockInfo . getDeadlockedThreads ( ) ; if ( 0 == deadlockedThreads . length ) { message . append ( " none " ) ; return message . toString ( ) ; } for ( final ThreadInfo threadInfo : deadlockedThreads ) { message . append ( threadPrefix ) . append ( "Thread '" ) . append ( threadInfo . getThreadName ( ) ) . append ( "' with state " ) . append ( threadInfo . getThreadState ( ) ) ; boolean firstElement = true ; for ( final StackTraceElement stackTraceElement : threadInfo . getStackTrace ( ) ) { message . append ( stackElementPrefix ) . append ( "at " ) . append ( stackTraceElement ) ; if ( firstElement ) { final String waitingLockString = deadlockInfo . getWaitingLockString ( threadInfo ) ; if ( waitingLockString != null ) { message . append ( stackElementPrefix ) . append ( "- waiting to lock: " ) . append ( waitingLockString ) ; } firstElement = false ; } for ( final MonitorInfo info : deadlockInfo . getMonitorLockedElements ( threadInfo , stackTraceElement ) ) { message . append ( stackElementPrefix ) . append ( "- locked: " ) . append ( info ) ; } } for ( final LockInfo lockInfo : threadInfo . getLockedSynchronizers ( ) ) { message . append ( stackElementPrefix ) . append ( "* holds locked synchronizer: " ) . append ( lockInfo ) ; } } return message . toString ( ) ; }
Produces a String representation of threads that are deadlocked including lock information .