1 7 8 package java.lang.management; 9 10 import javax.management.openmbean.CompositeData ; 11 import sun.management.ThreadInfoCompositeData; 12 13 61 62 public class ThreadInfo { 63 private final String threadName; 64 private final long threadId; 65 private final long blockedTime; 66 private final long blockedCount; 67 private final long waitedTime; 68 private final long waitedCount; 69 private final String lockName; 70 private final long lockOwnerId; 71 private final String lockOwnerName; 72 private final boolean inNative; 73 private final boolean suspended; 74 private final Thread.State threadState; 75 private final StackTraceElement [] stackTrace; 76 77 91 private ThreadInfo(Thread t, int state, Object lockObj, Thread lockOwner, 92 long blockedCount, long blockedTime, 93 long waitedCount, long waitedTime, 94 StackTraceElement [] stackTrace) { 95 this.threadId = t.getId(); 96 this.threadName = t.getName(); 97 this.threadState = 98 sun.management.ManagementFactory.toThreadState(state); 99 this.suspended = 100 sun.management.ManagementFactory.isThreadSuspended(state); 101 this.inNative = 102 sun.management.ManagementFactory.isThreadRunningNative(state); 103 this.blockedCount = blockedCount; 104 this.blockedTime = blockedTime; 105 this.waitedCount = waitedCount; 106 this.waitedTime = waitedTime; 107 108 if (lockObj == null) { 109 this.lockName = null; 110 } else { 111 this.lockName = 112 lockObj.getClass().getName() + '@' + 113 Integer.toHexString(System.identityHashCode(lockObj)); 114 } 115 if (lockOwner == null) { 116 this.lockOwnerId = -1; 117 this.lockOwnerName = null; 118 } else {; 119 this.lockOwnerId = lockOwner.getId(); 120 this.lockOwnerName = lockOwner.getName(); 121 } 122 this.stackTrace = stackTrace; 123 } 124 125 129 private ThreadInfo(CompositeData cd) { 130 ThreadInfoCompositeData.validateCompositeData(cd); 131 132 threadId = ThreadInfoCompositeData.getThreadId(cd); 133 threadName = ThreadInfoCompositeData.getThreadName(cd); 134 blockedTime = ThreadInfoCompositeData.getBlockedTime(cd); 135 blockedCount = ThreadInfoCompositeData.getBlockedCount(cd); 136 waitedTime = ThreadInfoCompositeData.getWaitedTime(cd); 137 waitedCount = ThreadInfoCompositeData.getWaitedCount(cd); 138 lockName = ThreadInfoCompositeData.getLockName(cd); 139 lockOwnerId = ThreadInfoCompositeData.getLockOwnerId(cd); 140 lockOwnerName = ThreadInfoCompositeData.getLockOwnerName(cd); 141 threadState = ThreadInfoCompositeData.getThreadState(cd); 142 suspended = ThreadInfoCompositeData.isSuspended(cd); 143 inNative = ThreadInfoCompositeData.isInNative(cd); 144 stackTrace = ThreadInfoCompositeData.getStackTrace(cd); 145 } 146 147 148 153 public long getThreadId() { 154 return threadId; 155 } 156 157 162 public String getThreadName() { 163 return threadName; 164 } 165 166 171 public Thread.State getThreadState() { 172 return threadState; 173 } 174 175 200 public long getBlockedTime() { 201 return blockedTime; 202 } 203 204 214 public long getBlockedCount() { 215 return blockedCount; 216 } 217 218 245 public long getWaitedTime() { 246 return waitedTime; 247 } 248 249 260 public long getWaitedCount() { 261 return waitedCount; 262 } 263 264 294 public String getLockName() { 295 return lockName; 296 } 297 298 314 public long getLockOwnerId() { 315 return lockOwnerId; 316 } 317 318 334 public String getLockOwnerName() { 335 return lockOwnerName; 336 } 337 338 357 public StackTraceElement [] getStackTrace() { 358 if (stackTrace == null) { 359 return NO_STACK_TRACE; 360 } else { 361 return stackTrace; 362 } 363 } 364 365 373 public boolean isSuspended() { 374 return suspended; 375 } 376 377 387 public boolean isInNative() { 388 return inNative; 389 } 390 391 396 public String toString() { 397 return "Thread " + getThreadName() + " (Id = " + getThreadId() + ") " + 398 getThreadState() + " " + getLockName(); 399 } 400 401 508 public static ThreadInfo from(CompositeData cd) { 509 if (cd == null) { 510 return null; 511 } 512 513 if (cd instanceof ThreadInfoCompositeData) { 514 return ((ThreadInfoCompositeData) cd).getThreadInfo(); 515 } else { 516 return new ThreadInfo (cd); 517 } 518 } 519 520 private static final StackTraceElement [] NO_STACK_TRACE = 521 new StackTraceElement [0]; 522 } 523 | Popular Tags |