本文整理汇总了Java中java.lang.management.ThreadInfo类的典型用法代码示例。如果您正苦于以下问题:Java ThreadInfo类的具体用法?Java ThreadInfo怎么用?Java ThreadInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ThreadInfo类属于java.lang.management包,在下文中一共展示了ThreadInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: waitUntilBlockingOnObject
import java.lang.management.ThreadInfo; //导入依赖的package包/类
/**
* Waits until {@link TestThread} is in the certain {@link State}
* and blocking on {@code object}.
*
* @param state The thread state
* @param object The object to block on
*/
public void waitUntilBlockingOnObject(Thread.State state, Object object) {
String want = object == null ? null : object.getClass().getName() + '@'
+ Integer.toHexString(System.identityHashCode(object));
ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
while (isAlive()) {
ThreadInfo ti = tmx.getThreadInfo(getId());
if (ti.getThreadState() == state
&& (want == null || want.equals(ti.getLockName()))) {
return;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
}
示例2: printThread
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private static StringBuilder printThread(ThreadInfo ti, StringBuilder sb) {
sb.append("\"" + ti.getThreadName() + "\"" + " Id="
+ ti.getThreadId() + " in " + ti.getThreadState());
if (ti.getLockName() != null) {
sb.append(" waiting on lock=" + ti.getLockName());
}
if (ti.isSuspended()) {
sb.append(" (suspended)");
}
if (ti.isInNative()) {
sb.append(" (running in native)");
}
sb.append("\n");
if (ti.getLockOwnerName() != null) {
sb.append("\t owned by " + ti.getLockOwnerName() + " Id="
+ ti.getLockOwnerId()).append("\n");
}
return sb;
}
示例3: getThreadInfo
import java.lang.management.ThreadInfo; //导入依赖的package包/类
public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
verifyThreadIds(ids);
if (maxDepth < 0) {
throw new IllegalArgumentException(
"Invalid maxDepth parameter: " + maxDepth);
}
// ids has been verified to be non-null
// an empty array of ids should return an empty array of ThreadInfos
if (ids.length == 0) return new ThreadInfo[0];
Util.checkMonitorAccess();
ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls
if (maxDepth == Integer.MAX_VALUE) {
getThreadInfo1(ids, -1, infos);
} else {
getThreadInfo1(ids, maxDepth, infos);
}
return infos;
}
示例4: internStackTrace
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private void internStackTrace(ThreadInfo tinfo) {
if (steCache == null) {
return;
}
StackTraceElement[] stack = tinfo.getStackTrace();
for (int i = 0; i < stack.length; i++) {
StackTraceElement ste = stack[i];
WeakReference<StackTraceElement> oldStackRef = steCache.get(ste);
if (oldStackRef != null) {
stack[i] = oldStackRef.get();
assert stack[i] != null;
} else {
steCache.put(ste, new WeakReference(ste));
}
}
}
示例5: ThreadsIterator
import java.lang.management.ThreadInfo; //导入依赖的package包/类
public ThreadsIterator(final SabotContext dbContext, final OperatorContext context) {
this.dbContext = dbContext;
threadMXBean = ManagementFactory.getThreadMXBean();
final long[] ids = threadMXBean.getAllThreadIds();
final Iterator<Long> threadIdIterator = Longs.asList(ids).iterator();
this.threadInfoIterator = Iterators.filter(
Iterators.transform(threadIdIterator, new Function<Long, ThreadInfo>() {
@Override
public ThreadInfo apply(Long input) {
return threadMXBean.getThreadInfo(input, 100);
}
}),
Predicates.notNull());
logger.debug("number of threads = {}, number of cores = {}", ids.length, NUMBER_OF_CORES);
this.stats = dbContext.getWorkStatsProvider().get();
}
示例6: badNameCompositeData
import java.lang.management.ThreadInfo; //导入依赖的package包/类
public static void badNameCompositeData() throws Exception {
CompositeType ct =
new CompositeType("MyCompositeType",
"CompositeType for ThreadInfo",
badItemNames,
badItemNames,
validItemTypes);
CompositeData cd =
new CompositeDataSupport(ct,
badItemNames,
values);
try {
ThreadInfo info = ThreadInfo.from(cd);
} catch (IllegalArgumentException e) {
System.out.println("Expected exception: " +
e.getMessage());
return;
}
throw new RuntimeException(
"IllegalArgumentException not thrown");
}
示例7: printThreads
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private void printThreads(final StringBuilder sb, ThreadDump td) {
ThreadInfo[] threads = td.getThreads();
boolean goToSourceAvailable = GoToSource.isAvailable();
boolean jdk15 = td.isJDK15();
sb.append("<pre>"); // NOI18N
sb.append(" <b>Full thread dump: "); // NOI18N
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // NOI18N
sb.append(df.format(td.getTime()) + "</b><br><br>");
for (ThreadInfo thread : threads) {
if (thread != null) {
if (jdk15) {
print15Thread(sb, thread, goToSourceAvailable);
} else {
print16Thread(sb, thread, goToSourceAvailable);
}
}
}
sb.append("</pre>"); // NOI18N
}
示例8: print15Thread
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private void print15Thread(final StringBuilder sb, final ThreadInfo thread, boolean goToSourceAvailable) {
sb.append("<br>\"" + thread.getThreadName() + // NOI18N
"\" - Thread [email protected]" + thread.getThreadId() + "<br>"); // NOI18N
sb.append(" java.lang.Thread.State: " + thread.getThreadState()); // NOI18N
if (thread.getLockName() != null) {
sb.append(" on " + thread.getLockName()); // NOI18N
if (thread.getLockOwnerName() != null) {
sb.append(" owned by: " + thread.getLockOwnerName()); // NOI18N
}
}
sb.append("<br>"); // NOI18N
for (StackTraceElement st : thread.getStackTrace()) {
String stackElementText = htmlize(st.toString());
String stackEl = stackElementText;
if (goToSourceAvailable) {
String className = st.getClassName();
String method = st.getMethodName();
int lineNo = st.getLineNumber();
String stackUrl = OPEN_THREADS_URL + className + "|" + method + "|" + lineNo; // NOI18N
stackEl = "<a href=\"" + stackUrl + "\">" + stackElementText + "</a>"; // NOI18N
}
sb.append(" at ").append(stackEl).append("<br>"); // NOI18N
}
}
示例9: getThreadDumpHeader
import java.lang.management.ThreadInfo; //导入依赖的package包/类
/**
* Formats the thread dump header for one thread.
*
* @param ti the ThreadInfo describing the thread
* @return the formatted thread dump header
*/
private static String getThreadDumpHeader(ThreadInfo ti) {
StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"");
sb.append(" Id=" + ti.getThreadId());
sb.append(" cpu=" + threadMXBean.getThreadCpuTime(ti.getThreadId()) +
" ns");
sb.append(" usr=" + threadMXBean.getThreadUserTime(ti.getThreadId()) +
" ns");
sb.append(" blocked " + ti.getBlockedCount() + " for " +
ti.getBlockedTime() + " ms");
sb.append(" waited " + ti.getWaitedCount() + " for " +
ti.getWaitedTime() + " ms");
if (ti.isSuspended()) {
sb.append(" (suspended)");
}
if (ti.isInNative()) {
sb.append(" (running in native)");
}
sb.append(CRLF);
sb.append(INDENT3 + "java.lang.Thread.State: " + ti.getThreadState());
sb.append(CRLF);
return sb.toString();
}
示例10: threadExists
import java.lang.management.ThreadInfo; //导入依赖的package包/类
/**
* This method return true/false if a Thread, given by it's name exists or not.
* @param threadName2LookAt the name of the thread
* @return boolean true, if the named thread exists
*/
public boolean threadExists(String threadName2LookAt) {
boolean exists = false;
long[] jvmThreadIDs = threadXB.getAllThreadIds();
ThreadInfo[] jvmThreadInfo = threadXB.getThreadInfo(jvmThreadIDs);
for (int i = 0; i < jvmThreadInfo.length; i++) {
String threadName = jvmThreadInfo[i].getThreadName();
if (threadName.equalsIgnoreCase(threadName2LookAt)) {
exists = true;
break;
}
}
return exists;
}
示例11: isMonitorLockHeldByThread
import java.lang.management.ThreadInfo; //导入依赖的package包/类
public static boolean isMonitorLockHeldByThread(Object o, Thread t) {
int oihc = System.identityHashCode(o);
ThreadMXBean tmxbean = ManagementFactory.getThreadMXBean();
ThreadInfo[] tinfos = tmxbean.dumpAllThreads(true, false);
for (ThreadInfo ti : tinfos) {
if (!(t != null && t.getId() != ti.getThreadId())) {
for (MonitorInfo mi : ti.getLockedMonitors()) {
if (mi.getIdentityHashCode() == oihc) {
return true;
}
}
}
}
return false;
}
示例12: getThreadUsage
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private void getThreadUsage(MetricsRecordBuilder rb) {
int threadsNew = 0;
int threadsRunnable = 0;
int threadsBlocked = 0;
int threadsWaiting = 0;
int threadsTimedWaiting = 0;
int threadsTerminated = 0;
long threadIds[] = threadMXBean.getAllThreadIds();
for (ThreadInfo threadInfo : threadMXBean.getThreadInfo(threadIds, 0)) {
if (threadInfo == null) continue; // race protection
switch (threadInfo.getThreadState()) {
case NEW: threadsNew++; break;
case RUNNABLE: threadsRunnable++; break;
case BLOCKED: threadsBlocked++; break;
case WAITING: threadsWaiting++; break;
case TIMED_WAITING: threadsTimedWaiting++; break;
case TERMINATED: threadsTerminated++; break;
}
}
rb.addGauge(ThreadsNew, threadsNew)
.addGauge(ThreadsRunnable, threadsRunnable)
.addGauge(ThreadsBlocked, threadsBlocked)
.addGauge(ThreadsWaiting, threadsWaiting)
.addGauge(ThreadsTimedWaiting, threadsTimedWaiting)
.addGauge(ThreadsTerminated, threadsTerminated);
}
示例13: run
import java.lang.management.ThreadInfo; //导入依赖的package包/类
@Override
public void run() {
while(goOn) {
try {
long[] ids = ManagementFactory.getThreadMXBean().findDeadlockedThreads();
checkCount.incrementAndGet();
ids = ids == null ? new long[0] : ids;
if (ids.length == 1) {
throw new RuntimeException("Found 1 deadlocked thread: "+ids[0]);
} else if (ids.length > 0) {
ThreadInfo[] infos = ManagementFactory.getThreadMXBean()
.getThreadInfo(ids, Integer.MAX_VALUE);
System.err.println("Found "+ids.length+" deadlocked threads: ");
for (ThreadInfo inf : infos) {
System.err.println(inf.toString());
}
throw new RuntimeException("Found "+ids.length+" deadlocked threads");
}
Thread.sleep(100);
} catch(InterruptedException | RuntimeException x) {
fail(x);
}
}
}
示例14: countTimerThreads
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private static int countTimerThreads() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
int count = 0;
ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 20);
for (ThreadInfo info : infos) {
if (info == null) continue;
for (StackTraceElement elem : info.getStackTrace()) {
if (elem.getClassName().contains("Timer")) {
count++;
break;
}
}
}
return count;
}
示例15: printThreadInfo
import java.lang.management.ThreadInfo; //导入依赖的package包/类
private static void printThreadInfo(ThreadInfo ti, PrintWriter out) {
// print thread information
printThread(ti, out);
// print stack trace with locks
StackTraceElement[] stacktrace = ti.getStackTrace();
MonitorInfo[] monitors = ti.getLockedMonitors();
for (int i = 0; i < stacktrace.length; i++) {
StackTraceElement ste = stacktrace[i];
out.println(INDENT + "at " + ste.toString());
for (MonitorInfo mi : monitors) {
if (mi.getLockedStackDepth() == i) {
out.println(INDENT + " - locked " + mi);
}
}
}
out.println();
}