3535import java .util .Locale ;
3636import java .util .Map ;
3737import java .util .concurrent .TimeUnit ;
38+ import java .util .function .ToLongFunction ;
3839
3940public class HotThreads {
4041
@@ -187,19 +188,19 @@ private String innerDetect() throws Exception {
187188 List <MyThreadInfo > hotties = new ArrayList <>(threadInfos .values ());
188189 final int busiestThreads = Math .min (this .busiestThreads , hotties .size ());
189190 // skip that for now
190- CollectionUtil . introSort ( hotties , new Comparator <MyThreadInfo >() {
191- @ Override
192- public int compare ( MyThreadInfo o1 , MyThreadInfo o2 ) {
193- if ("cpu " .equals (type )) {
194- return ( int ) ( o2 . cpuTime - o1 . cpuTime ) ;
195- } else if ("wait " .equals (type )) {
196- return ( int ) ( o2 . waitedTime - o1 . waitedTime ) ;
197- } else if ( "block" . equals ( type )) {
198- return ( int ) ( o2 . blockedTime - o1 . blockedTime );
199- }
200- throw new IllegalArgumentException ( "expected thread type to be either 'cpu', 'wait', or 'block', but was " + type );
201- }
202- });
191+ final ToLongFunction <MyThreadInfo > getter ;
192+ if ( "cpu" . equals ( type )) {
193+ getter = o -> o . cpuTime ;
194+ } else if ("wait " .equals (type )) {
195+ getter = o -> o . waitedTime ;
196+ } else if ("block " .equals (type )) {
197+ getter = o -> o . blockedTime ;
198+ } else {
199+ throw new IllegalArgumentException ( "expected thread type to be either 'cpu', 'wait', or 'block', but was " + type );
200+ }
201+
202+ CollectionUtil . introSort ( hotties , Comparator . comparingLong ( getter ). reversed ());
203+
203204 // analyse N stack traces for M busiest threads
204205 long [] ids = new long [busiestThreads ];
205206 for (int i = 0 ; i < busiestThreads ; i ++) {
@@ -215,14 +216,7 @@ public int compare(MyThreadInfo o1, MyThreadInfo o2) {
215216 Thread .sleep (threadElementsSnapshotDelay .millis ());
216217 }
217218 for (int t = 0 ; t < busiestThreads ; t ++) {
218- long time = 0 ;
219- if ("cpu" .equals (type )) {
220- time = hotties .get (t ).cpuTime ;
221- } else if ("wait" .equals (type )) {
222- time = hotties .get (t ).waitedTime ;
223- } else if ("block" .equals (type )) {
224- time = hotties .get (t ).blockedTime ;
225- }
219+ long time = getter .applyAsLong (hotties .get (t ));
226220 String threadName = null ;
227221 for (ThreadInfo [] info : allInfos ) {
228222 if (info != null && info [t ] != null ) {
0 commit comments