本文整理汇总了Java中java.lang.management.ThreadInfo.getStackTrace方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadInfo.getStackTrace方法的具体用法?Java ThreadInfo.getStackTrace怎么用?Java ThreadInfo.getStackTrace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.management.ThreadInfo
的用法示例。
在下文中一共展示了ThreadInfo.getStackTrace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: countThreads
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/**
* Count the number of threads that have a stack frame containing
* the given string
*/
protected static int countThreads(String search) {
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(search)) {
count++;
break;
}
}
}
return count;
}
示例2: render
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
@Override
protected void render(Block html) {
set(TITLE, join("Angel ExecutorsBlock"));
TBODY<TABLE<Hamlet>> tbody =
html.h1("ExecutorsBlock").table("#jobs").thead().tr().th(_TH, "id").th(_TH, "name")
.th(_TH, "state").th(_TH, "stacktrace")._()._().tbody();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfo = threadMXBean.dumpAllThreads(true, true);
StringBuilder stackTraceString;
for (ThreadInfo t : threadInfo) {
stackTraceString = new StringBuilder();
StackTraceElement[] stackTrace = t.getStackTrace();
for (StackTraceElement s : stackTrace) {
stackTraceString.append(s.toString()).append("\n");
}
tbody.tr().td(String.valueOf(t.getThreadId())).td(String.valueOf(t.getThreadName()))
.td(String.valueOf(t.getThreadState())).td(String.valueOf(stackTraceString.toString()))._();
}
tbody._()._();
}
示例3: getThreadStack
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/**
* Gets thread stack.
*
* @return the thread stack
*/
private String getThreadStack()
{
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfo = threadMXBean.dumpAllThreads(true, true);
StringBuilder stackTraceString= new StringBuilder("ParameterServer\n");
StringBuilder infoBlock= new StringBuilder("\n");
for(ThreadInfo t : threadInfo)
{
infoBlock = new StringBuilder("\n\n");
infoBlock.append("threadid: ").append(t.getThreadId()).append(" threadname: ").append(t.getThreadName()).append(" threadstate: ").append(t.getThreadState()).append("\n");
for(StackTraceElement stackTraceElement : t.getStackTrace())
{
infoBlock.append(" ").append(stackTraceElement.toString()).append("\n");
}
stackTraceString.append(infoBlock).append("\n\n");
}
return stackTraceString.toString();
}
示例4: 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();
}
示例5: findStack
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
private boolean findStack(final List<String[]> stack, final ThreadInfo[] threads) {
for (ThreadInfo t : threads) {
StackTraceElement[] els = t.getStackTrace();
if (els == null || els.length < stack.size()) {
continue;
}
int j=0;
for (; j<stack.size(); j++) {
StackTraceElement el = els[els.length - j - 1];
String[] method = stack.get(j);
if (!el.getClassName().equals(method[0]) || !el.getMethodName().equals(method[1])) {
break; // try next thread
}
}
if (j == stack.size()) { // match
return true;
}
}
return false;
}
示例6: 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;
}
示例7: countThreads
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/**
* Count the number of threads that have a stack frame containing
* the given string
*/
private static int countThreads(String search) {
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(search)) {
count++;
break;
}
}
}
return count;
}
示例8: printThreadInfo
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
public static void printThreadInfo(PrintWriter stream, String title) {
int STACK_DEPTH = 20;
boolean contention = threadBean.isThreadContentionMonitoringEnabled();
long[] threadIds = threadBean.getAllThreadIds();
stream.println("Process Thread Dump: " + title);
stream.println(threadIds.length + " active threads");
for (long tid : threadIds) {
ThreadInfo info = threadBean.getThreadInfo(tid, 20);
if (info == null) {
stream.println(" Inactive");
} else {
stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
Thread.State state = info.getThreadState();
stream.println(" State: " + state);
stream.println(" Blocked count: " + info.getBlockedCount());
stream.println(" Waited count: " + info.getWaitedCount());
if (contention) {
stream.println(" Blocked time: " + info.getBlockedTime());
stream.println(" Waited time: " + info.getWaitedTime());
}
if (state == Thread.State.WAITING) {
stream.println(" Waiting on " + info.getLockName());
} else if (state == Thread.State.BLOCKED) {
stream.println(" Blocked on " + info.getLockName());
stream.println(" Blocked by "
+ getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
}
stream.println(" Stack:");
for (StackTraceElement frame : info.getStackTrace())
stream.println(" " + frame.toString());
}
}
stream.flush();
}
示例9: isIdleThread
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
private static boolean isIdleThread(ThreadInfo threadInfo) {
String threadName = threadInfo.getThreadName();
// NOTE: these are likely JVM dependent
if (threadName.equals("Signal Dispatcher") ||
threadName.equals("Finalizer") ||
threadName.equals("Reference Handler")) {
return true;
}
for (StackTraceElement frame : threadInfo.getStackTrace()) {
String className = frame.getClassName();
String methodName = frame.getMethodName();
if (className.equals("java.util.concurrent.ThreadPoolExecutor") &&
methodName.equals("getTask")) {
return true;
}
if (className.equals("sun.nio.ch.SelectorImpl") &&
methodName.equals("select")) {
return true;
}
if (className.equals("org.elasticsearch.threadpool.ThreadPool$EstimatedTimeThread") &&
methodName.equals("run")) {
return true;
}
if (className.equals("org.elasticsearch.indices.ttl.IndicesTTLService$Notifier") &&
methodName.equals("await")) {
return true;
}
if (className.equals("java.util.concurrent.LinkedTransferQueue") &&
methodName.equals("poll")) {
return true;
}
}
return false;
}
示例10: printThreadInfo
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/**
* Print all of the thread's information and stack traces.
*
* @param stream the stream to
* @param title a string title for the stack trace
*/
private static void printThreadInfo(PrintStream stream,
String title) {
final int STACK_DEPTH = 20;
boolean contention = threadBean.isThreadContentionMonitoringEnabled();
long[] threadIds = threadBean.getAllThreadIds();
stream.println("Process Thread Dump: " + title);
stream.println(threadIds.length + " active threads");
for (long tid: threadIds) {
ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
if (info == null) {
stream.println(" Inactive");
continue;
}
stream.println("Thread " +
getTaskName(info.getThreadId(),
info.getThreadName()) + ":");
Thread.State state = info.getThreadState();
stream.println(" State: " + state);
stream.println(" Blocked count: " + info.getBlockedCount());
stream.println(" Waited count: " + info.getWaitedCount());
if (contention) {
stream.println(" Blocked time: " + info.getBlockedTime());
stream.println(" Waited time: " + info.getWaitedTime());
}
if (state == Thread.State.WAITING) {
stream.println(" Waiting on " + info.getLockName());
} else if (state == Thread.State.BLOCKED) {
stream.println(" Blocked on " + info.getLockName());
stream.println(" Blocked by " +
getTaskName(info.getLockOwnerId(),
info.getLockOwnerName()));
}
stream.println(" Stack:");
for (StackTraceElement frame: info.getStackTrace()) {
stream.println(" " + frame.toString());
}
}
stream.flush();
}
示例11: writeSample
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
void writeSample(ThreadInfo[] infos, long time, long selfThreadId) throws IOException {
List<Long> sameT = new ArrayList();
List<ThreadInfo> newT = new ArrayList();
List<Long> tids = new ArrayList();
for (ThreadInfo tinfo : infos) {
long id;
if (tinfo == null) continue; // ignore null ThreadInfo
id = tinfo.getThreadId();
if (id != selfThreadId) { // ignore sampling thread
Long tid = Long.valueOf(tinfo.getThreadId());
ThreadInfo lastThread = lastThreadInfos.get(tid);
tids.add(tid);
if (lastThread != null) {
if (lastThread.getThreadState().equals(tinfo.getThreadState())) {
StackTraceElement[] lastStack = lastThread.getStackTrace();
StackTraceElement[] stack = tinfo.getStackTrace();
if (Arrays.deepEquals(lastStack, stack)) {
sameT.add(tid);
continue;
}
}
}
internStackTrace(tinfo);
newT.add(tinfo);
lastThreadInfos.put(tid, tinfo);
}
}
addSample(new Sample(time, sameT, newT));
// remove dead threads
Set<Long> ids = new HashSet(lastThreadInfos.keySet());
ids.removeAll(tids);
lastThreadInfos.keySet().removeAll(ids);
}
示例12: getThreadDebugData
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/** Returns an array list of debug information for all Java threads; useful for trying to diagnose the cause of odd behaviour.
*
* @return An array list of debug information
*/
public static ArrayList<String> getThreadDebugData()
{
ArrayList<String> strings = new ArrayList<String>();
strings.add("------------------------------------------------------------");
strings.add("Debugging information for Java virtual machine");
ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
for (ThreadInfo thread : threads)
{
if (thread.getThreadState() != State.WAITING)
{
strings.add("------------------------------------------------------------");
strings.add(thread.getThreadName());
strings.add("\tPID: " + thread.getThreadId() + " | Suspended: " + thread.isSuspended() + " | Native: " + thread.isInNative() + " | State: " + thread.getThreadState());
if (thread.getLockedMonitors().length != 0)
{
strings.add("\tThread is waiting on monitor(s):");
for (MonitorInfo monitor : thread.getLockedMonitors())
{
strings.add("\t\tLocked on:" + monitor.getLockedStackFrame());
}
}
strings.add("\tStack trace:");
StackTraceElement[] stack = thread.getStackTrace();
for (int line = 0; line < stack.length; line++)
{
strings.add("\t\t" + stack[line].toString());
}
}
}
strings.add("------------------------------------------------------------");
strings.add("End of debugging information");
strings.add("------------------------------------------------------------");
return strings;
}
示例13: appendThreadInfo
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/**
* Print all of the thread's information and stack traces.
*
* @param sb
* @param info
* @param indent
*/
public static void appendThreadInfo(StringBuilder sb,
ThreadInfo info,
String indent) {
boolean contention = threadBean.isThreadContentionMonitoringEnabled();
if (info == null) {
sb.append(indent).append("Inactive (perhaps exited while monitoring was done)\n");
return;
}
String taskName = getTaskName(info.getThreadId(), info.getThreadName());
sb.append(indent).append("Thread ").append(taskName).append(":\n");
Thread.State state = info.getThreadState();
sb.append(indent).append(" State: ").append(state).append("\n");
sb.append(indent).append(" Blocked count: ").append(info.getBlockedCount()).append("\n");
sb.append(indent).append(" Waited count: ").append(info.getWaitedCount()).append("\n");
if (contention) {
sb.append(indent).append(" Blocked time: " + info.getBlockedTime()).append("\n");
sb.append(indent).append(" Waited time: " + info.getWaitedTime()).append("\n");
}
if (state == Thread.State.WAITING) {
sb.append(indent).append(" Waiting on ").append(info.getLockName()).append("\n");
} else if (state == Thread.State.BLOCKED) {
sb.append(indent).append(" Blocked on ").append(info.getLockName()).append("\n");
sb.append(indent).append(" Blocked by ").append(
getTaskName(info.getLockOwnerId(), info.getLockOwnerName())).append("\n");
}
sb.append(indent).append(" Stack:").append("\n");
for (StackTraceElement frame: info.getStackTrace()) {
sb.append(indent).append(" ").append(frame.toString()).append("\n");
}
}
示例14: printThreadInfo
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
/**
* Print all of the thread's information and stack traces.
*
* @param stream the stream to
* @param title a string title for the stack trace
*/
public static void printThreadInfo(PrintWriter stream,
String title) {
final int STACK_DEPTH = 20;
boolean contention = threadBean.isThreadContentionMonitoringEnabled();
long[] threadIds = threadBean.getAllThreadIds();
stream.println("Process Thread Dump: " + title);
stream.println(threadIds.length + " active threads");
for (long tid: threadIds) {
ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
if (info == null) {
stream.println(" Inactive");
continue;
}
stream.println("Thread " +
getTaskName(info.getThreadId(),
info.getThreadName()) + ":");
Thread.State state = info.getThreadState();
stream.println(" State: " + state);
stream.println(" Blocked count: " + info.getBlockedCount());
stream.println(" Waited count: " + info.getWaitedCount());
if (contention) {
stream.println(" Blocked time: " + info.getBlockedTime());
stream.println(" Waited time: " + info.getWaitedTime());
}
if (state == Thread.State.WAITING) {
stream.println(" Waiting on " + info.getLockName());
} else if (state == Thread.State.BLOCKED) {
stream.println(" Blocked on " + info.getLockName());
stream.println(" Blocked by " +
getTaskName(info.getLockOwnerId(),
info.getLockOwnerName()));
}
stream.println(" Stack:");
for (StackTraceElement frame: info.getStackTrace()) {
stream.println(" " + frame.toString());
}
}
stream.flush();
}
示例15: dumpThreads
import java.lang.management.ThreadInfo; //导入方法依赖的package包/类
public static void dumpThreads(String file) throws IOException {
StringBuilder dump = new StringBuilder();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
for (ThreadInfo threadInfo : threadInfos) {
dump.append('"');
dump.append(threadInfo.getThreadName());
dump.append("\" ");
final Thread.State state = threadInfo.getThreadState();
dump.append("\n java.lang.Thread.State: ");
dump.append(state);
final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
for (final StackTraceElement stackTraceElement : stackTraceElements) {
dump.append("\n at ");
dump.append(stackTraceElement);
}
dump.append("\n\n");
}
ByteArrayInputStream bais = new ByteArrayInputStream(dump.toString().getBytes(Charset.defaultCharset()));
try(FileOutputStream fos = new FileOutputStream(file)) {
byte[] buffer = new byte[1024];
int read;
while((read = bais.read(buffer)) > -1) {
fos.write(buffer, 0, read);
}
}
}