private List < Thread > getThreadsInGroup ( ThreadGroup group ) { final int activeThreadCount = group . activeCount ( ) ; // this is just an estimate int threadArraySize = Math . max ( activeThreadCount * 2 , 100 ) ; for ( int loopCount = 0 ; loopCount < 5 ; loopCount ++ ) { Thread [ ] threads = new Thread [ threadArraySize ] ; int enumCount = group . enumerate ( threads ) ; if ( enumCount < threadArraySize ) { return Arrays . asList ( threads ) . subList ( 0 , enumCount ) ; } // if there are too many threads to fit into the array, enumerate's result // is >= the array's length; therefore we can't trust that it returned all // the threads. Try again. threadArraySize += 100 ; } // threads are proliferating too fast for us. Bail before we get into // trouble. return Collections . emptyList ( ) ; }
Returns all active threads belonging to a thread group .