2929import java .lang .management .ManagementFactory ;
3030import java .lang .management .ThreadInfo ;
3131import java .lang .management .ThreadMXBean ;
32+ import java .util .logging .Level ;
33+ import java .util .logging .Logger ;
3234import javax .management .InstanceNotFoundException ;
3335import javax .management .MBeanException ;
3436import javax .management .MBeanServerConnection ;
4244 */
4345public class ThreadsCPU {
4446 private static final ObjectName THREAD_NAME = getThreadName ();
47+ private static final Logger LOGGER = Logger .getLogger (ThreadsCPU .class .getName ());
4548
4649 private final ThreadMXBean threadBean ;
4750 private final MBeanServerConnection connection ;
51+ private boolean useBulkOperation = true ;
4852
4953 public ThreadsCPU (ThreadMXBean bean , MBeanServerConnection conn ) {
5054 threadBean = bean ;
@@ -54,9 +58,26 @@ public ThreadsCPU(ThreadMXBean bean, MBeanServerConnection conn) {
5458 public ThreadsCPUInfo getThreadsCPUInfo () throws MBeanException , ReflectionException , IOException , InstanceNotFoundException {
5559 long [] ids = threadBean .getAllThreadIds ();
5660 ThreadInfo [] tids = threadBean .getThreadInfo (ids );
57- Object [] args = new Object [] {ids };
58- String [] sigs = new String [] {"[J" }; // NOI18N
59- long [] tinfo = (long [])connection .invoke (THREAD_NAME , "getThreadCpuTime" , args , sigs ); // NOI18N
61+ long [] tinfo ;
62+
63+ if (useBulkOperation ) {
64+ Object [] args = new Object [] {ids };
65+ String [] sigs = new String [] {"[J" }; // NOI18N
66+
67+ try {
68+ tinfo = (long [])connection .invoke (THREAD_NAME , "getThreadCpuTime" , args , sigs ); // NOI18N
69+ } catch (javax .management .ReflectionException ex ) {
70+ LOGGER .log (Level .INFO , "getThreadCpuTime failed" , ex );
71+ useBulkOperation = false ;
72+ return getThreadsCPUInfo ();
73+ }
74+ } else {
75+ tinfo = new long [ids .length ];
76+
77+ for (int i = 0 ; i < ids .length ; i ++) {
78+ tinfo [i ] = threadBean .getThreadCpuTime (ids [i ]);
79+ }
80+ }
6081 long time = System .currentTimeMillis ();
6182
6283 return new ThreadsCPUInfo (time ,tids ,tinfo );
0 commit comments