private static Thread findActiveThreadByName ( ThreadGroup group , String threadName ) { Thread result = null ; // Get threads in 'group' int numThreads = group . activeCount ( ) ; Thread [ ] threads = new Thread [ numThreads * 2 ] ; numThreads = group . enumerate ( threads , false ) ; // Enumerate each thread in 'group' for ( int i = 0 ; i < numThreads ; i ++ ) { if ( threads [ i ] . getName ( ) . equals ( threadName ) ) { return threads [ i ] ; } } // Get thread subgroups of 'group' int numGroups = group . activeGroupCount ( ) ; ThreadGroup [ ] groups = new ThreadGroup [ numGroups * 2 ] ; numGroups = group . enumerate ( groups , false ) ; // Recursively visit each subgroup for ( int i = 0 ; i < numGroups ; i ++ ) { result = findActiveThreadByName ( groups [ i ] , threadName ) ; if ( result != null ) { break ; } } return result ; }
This method recursively visits all thread groups under group searching for the active thread with name threadName .