本文整理汇总了Java中java.lang.management.ThreadMXBean.getAllThreadIds方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadMXBean.getAllThreadIds方法的具体用法?Java ThreadMXBean.getAllThreadIds怎么用?Java ThreadMXBean.getAllThreadIds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.management.ThreadMXBean
的用法示例。
在下文中一共展示了ThreadMXBean.getAllThreadIds方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAllThreadInfo
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
public static List<TInfo> getAllThreadInfo(){
List<TInfo> threads = new ArrayList<>();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] ids = threadBean.getAllThreadIds();
ThreadInfo[] infos = threadBean.getThreadInfo(ids);
for (ThreadInfo info : infos){
long id = info.getThreadId();
TInfo tInfo = new TInfo();
tInfo.name = info.getThreadName();
tInfo.id = id;
tInfo.state = info.getThreadState();
tInfo.cpuTime = threadBean.getThreadCpuTime(id);
threads.add(tInfo);
}
Collections.sort(threads,new Comparator<TInfo>() {
@Override
public int compare(TInfo o1, TInfo o2) {
return Long.compare(o2.cpuTime,o1.cpuTime);
}
});
return threads;
}
示例2: dump
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
public static ThreadInfo[] dump(int maxDepth,boolean onlyDeadLock){
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] ids ;
if(onlyDeadLock) {
ids = threadBean.findDeadlockedThreads();
}else {
ids = threadBean.getAllThreadIds();
}
if(ids !=null) {
return threadBean.getThreadInfo(ids, maxDepth);
}
else {
return null;
}
}
示例3: getAllThreadIDsExceptMain
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* @return Set of the IDs of all threads that are started except the "main" thread.
*/
public Set<Long> getAllThreadIDsExceptMain() {
ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
// get the IDs of all live threads.
long[] threadIDs = thbean.getAllThreadIds();
Set<Long> res = new HashSet<Long>();
// get the IDs of all threads except main
for (long id : threadIDs) {
Thread t = getThread(id);
if (t != null && !"main".equals(t.getName())) {
res.add(id);
}
}
return res;
}
示例4: main
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
public static void main(String args[]) {
// get top-level thread group
ThreadGroup top = Thread.currentThread().getThreadGroup();
ThreadGroup parent;
do {
parent = top.getParent();
if (parent != null) top = parent;
} while (parent != null);
// get the thread count
int activeCount = top.activeCount();
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
int threadCount = threadBean.getThreadCount();
long[] threadIds = threadBean.getAllThreadIds();
System.out.println("ThreadGroup: " + activeCount + " active thread(s)");
System.out.println("Thread: " + stackTraces.size() + " stack trace(s) returned");
System.out.println("ThreadMXBean: " + threadCount + " live threads(s)");
System.out.println("ThreadMXBean: " + threadIds.length + " thread Id(s)");
// check results are consistent
boolean failed = false;
if (activeCount != stackTraces.size()) failed = true;
if (activeCount != threadCount) failed = true;
if (activeCount != threadIds.length) failed = true;
if (failed) {
throw new RuntimeException("inconsistent results");
}
}
示例5: main
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
public static void main(String args[]) {
// Bug id : JDK-8151797
// Use a lambda expression so that call-site cleaner thread is started
Runnable printLambda = () -> {System.out.println("Starting Test");};
printLambda.run();
// get top-level thread group
ThreadGroup top = Thread.currentThread().getThreadGroup();
ThreadGroup parent;
do {
parent = top.getParent();
if (parent != null) top = parent;
} while (parent != null);
// get the thread count
int activeCount = top.activeCount();
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
int threadCount = threadBean.getThreadCount();
long[] threadIds = threadBean.getAllThreadIds();
System.out.println("ThreadGroup: " + activeCount + " active thread(s)");
System.out.println("Thread: " + stackTraces.size() + " stack trace(s) returned");
System.out.println("ThreadMXBean: " + threadCount + " live threads(s)");
System.out.println("ThreadMXBean: " + threadIds.length + " thread Id(s)");
// check results are consistent
boolean failed = false;
if (activeCount != stackTraces.size()) failed = true;
if (activeCount != threadCount) failed = true;
if (activeCount != threadIds.length) failed = true;
if (failed) {
throw new RuntimeException("inconsistent results");
}
}
示例6: doThreadMeasurement
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* Does the thread measurement. For this, a single thread will be started and the results
* will be transfered to the specified {@link ThreadProtocolReceiver}.
*
* @see ThreadProtocolReceiver
*
* @param timestamp the time stamp of the initial request
* @param threadProtocolReceiver the {@link ThreadProtocolReceiver}
*/
public static void doThreadMeasurement(final long timestamp, final ThreadProtocolReceiver threadProtocolReceiver) {
if (timestamp!=threadMeasurementLastTimeStamp) {
// --- Remind this time stamp in order to avoid double work -------
threadMeasurementLastTimeStamp = timestamp;
// --- Start Thread to do0 the work -------------------------------
Runnable threadMeasurement = new Runnable() {
@Override
public void run() {
// --- Create a protocol instance -------------------------
ThreadProtocol tp = new ThreadProtocol(timestamp, getLoadCPU());
// --- Configure ThreadMXBean if possible and needed ------
ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
if (tmxb.isThreadCpuTimeSupported()==true){
if(tmxb.isThreadCpuTimeEnabled()==false){
tmxb.setThreadCpuTimeEnabled(true);
}
} else{
System.err.println("ThreadTimeMeasurement not supported !!");
threadProtocolReceiver.receiveThreadProtocol(null);
return;
}
// --- Do measurement -------------------------------------
String threadName;
long nanosCpuTime = 0L;
long nanosUserTime = 0L;
long factorMiliseconds = 1000000;
long[] threadIDs = tmxb.getAllThreadIds();
for (int i = 0; i < threadIDs.length; i++) {
long threadID = threadIDs[i];
nanosCpuTime = tmxb.getThreadCpuTime(threadID);
nanosUserTime = tmxb.getThreadUserTime(threadID);
if (nanosCpuTime==-1 || nanosUserTime==-1) {
continue; // Thread died
}
threadName = tmxb.getThreadInfo(threadID).getThreadName();
// --- add times, converted to milliseconds, to thread-Protocol
tp.getThreadDetails().add(new ThreadDetail(threadName, (nanosCpuTime/factorMiliseconds), (nanosUserTime/factorMiliseconds)));
}
// --- Send protocol to the requester of the measurement --
threadProtocolReceiver.receiveThreadProtocol(tp);
}
};
// --- Start measurement thread -----------------------------------
threadMeasurement.run();
}
}
示例7: printServerState
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
@Override
public String printServerState() {
final long ONE_MB = 1024 * 1024;
String newLine = "\n";
StringBuilder result = new StringBuilder();
try {
long freeMemory = Runtime.getRuntime().freeMemory() / ONE_MB; // 空闲内存
long totalMemory = Runtime.getRuntime().totalMemory() / ONE_MB; // 当前内存
long maxMemory = Runtime.getRuntime().maxMemory() / ONE_MB; // 最大可使用内存
result.append(String.format("freeMemory: %s mb", freeMemory)).append(newLine);
result.append(String.format(String.format("usedMemory: %s mb", (totalMemory - freeMemory)))).append(newLine);
result.append(String.format("totalMemory: %s mb", totalMemory)).append(newLine);
result.append(String.format("maxMemory: %s mb", maxMemory)).append(newLine);
MemoryMXBean memoryMXBean = getMemoryMXBean();
result.append(String.format("heap memory used: %s mb", memoryMXBean.getHeapMemoryUsage().getUsed() / ONE_MB)).append(newLine);
result.append(String.format("heap memory usage: %s", memoryMXBean.getHeapMemoryUsage())).append(newLine);
result.append(String.format("nonHeap memory usage: %s", memoryMXBean.getNonHeapMemoryUsage())).append(newLine);
List<BufferPoolMXBean> buffMXBeans = (List<BufferPoolMXBean>) getPlatformMXBeans(BufferPoolMXBean.class);
for (BufferPoolMXBean buffMXBean : buffMXBeans) {
result.append(String.format("buffer pool[%s]: used %s mb, total %s mb", buffMXBean.getName(),
buffMXBean.getMemoryUsed() / ONE_MB, buffMXBean.getTotalCapacity() / ONE_MB)).append(newLine);
}
List<GarbageCollectorMXBean> gcMXBeans = getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcBean : gcMXBeans) {
result.append(String.format("%s 发生 %s 次 gc, gc 总共消耗 %s 毫秒", gcBean.getName(), gcBean.getCollectionCount(), gcBean.getCollectionTime())).append(newLine);
}
ThreadMXBean threadMXBean = getThreadMXBean();
int nThreadRun = 0;
int nThreadBlocked = 0;
int nThreadWaiting = 0;
for (long threadId : threadMXBean.getAllThreadIds()) {
ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId);
if (threadInfo.getThreadState() == Thread.State.RUNNABLE) {
nThreadRun++;
}
if (threadInfo.getThreadState() == Thread.State.BLOCKED) {
nThreadBlocked++;
}
if (threadInfo.getThreadState() == Thread.State.WAITING) {
nThreadWaiting++;
}
}
result.append(String.format("活跃线程数 %s, 阻塞线程数 %s, 等待线程数 %s", nThreadRun, nThreadBlocked, nThreadWaiting)).append(newLine);
} catch (Exception e) {
LoggerUtils.error("", e);
}
return result.toString();
}
示例8: doThreadUpdates
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
private void doThreadUpdates() {
ThreadMXBean threadMXBean =
ManagementFactory.getThreadMXBean();
long threadIds[] =
threadMXBean.getAllThreadIds();
ThreadInfo[] threadInfos =
threadMXBean.getThreadInfo(threadIds, 0);
int threadsNew = 0;
int threadsRunnable = 0;
int threadsBlocked = 0;
int threadsWaiting = 0;
int threadsTimedWaiting = 0;
int threadsTerminated = 0;
for (ThreadInfo threadInfo : threadInfos) {
// threadInfo is null if the thread is not alive or doesn't exist
if (threadInfo == null) continue;
Thread.State state = threadInfo.getThreadState();
if (state == NEW) {
threadsNew++;
}
else if (state == RUNNABLE) {
threadsRunnable++;
}
else if (state == BLOCKED) {
threadsBlocked++;
}
else if (state == WAITING) {
threadsWaiting++;
}
else if (state == TIMED_WAITING) {
threadsTimedWaiting++;
}
else if (state == TERMINATED) {
threadsTerminated++;
}
}
metrics.setMetric("threadsNew", threadsNew);
metrics.setMetric("threadsRunnable", threadsRunnable);
metrics.setMetric("threadsBlocked", threadsBlocked);
metrics.setMetric("threadsWaiting", threadsWaiting);
metrics.setMetric("threadsTimedWaiting", threadsTimedWaiting);
metrics.setMetric("threadsTerminated", threadsTerminated);
}