當前位置: 首頁>>代碼示例>>Java>>正文


Java ThreadMXBean類代碼示例

本文整理匯總了Java中java.lang.management.ThreadMXBean的典型用法代碼示例。如果您正苦於以下問題:Java ThreadMXBean類的具體用法?Java ThreadMXBean怎麽用?Java ThreadMXBean使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ThreadMXBean類屬於java.lang.management包,在下文中一共展示了ThreadMXBean類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: start

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
public static void start() {
	if (started) throw new IllegalStateException("Already Started.");
	OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
	ThreadMXBean thread = ManagementFactory.getThreadMXBean();
	Runtime r = Runtime.getRuntime();
	int mb = 0x100000;

	lastSystemTime = System.nanoTime();
	lastProcessCpuTime = calculateProcessCpuTime(os);

	Async.task("AsyncInfoMonitorThread", () -> {
		threadCount = thread.getThreadCount();
		availableProcessors = r.availableProcessors();
		freeMemory = Runtime.getRuntime().freeMemory() / mb;
		maxMemory = Runtime.getRuntime().maxMemory() / mb;
		totalMemory = Runtime.getRuntime().totalMemory() / mb;
		cpuUsage = calculateCpuUsage(os);
		vpsCPUUsage = getVpsCPUUsage(os);
		vpsFreeMemory = calculateVPSFreeMemory(os);
		vpsMaxMemory = calculateVPSMaxMemory(os);
		vpsUsedMemory = vpsMaxMemory - vpsFreeMemory;
	}, 1);
	started = true;
}
 
開發者ID:Mantaro,項目名稱:MantaroRPG,代碼行數:25,代碼來源:AsyncInfoMonitor.java

示例2: render

import java.lang.management.ThreadMXBean; //導入依賴的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._()._();


}
 
開發者ID:Tencent,項目名稱:angel,代碼行數:24,代碼來源:ExecutorsBlock.java

示例3: getThreadStack

import java.lang.management.ThreadMXBean; //導入依賴的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();
}
 
開發者ID:Tencent,項目名稱:angel,代碼行數:24,代碼來源:ParameterServerService.java

示例4: call

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
@Override
public String call() {
  Map<String, String> memoryMap = new HashMap<>();
  OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
  double cpu = osMxBean.getSystemLoadAverage();
  memoryMap.put("cpuLoad", String.format(doubleStringFormatter, round(cpu, doubleRoundPlaces)));

  ThreadMXBean threadmxBean = ManagementFactory.getThreadMXBean();
  int threadCount = threadmxBean.getThreadCount();
  memoryMap.put("cpuRunningThreads", String.valueOf(threadCount));

  MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
  MemoryUsage memHeapUsage = memBean.getHeapMemoryUsage();
  MemoryUsage nonHeapUsage = memBean.getNonHeapMemoryUsage();
  memoryMap.put("heapInit", String.valueOf(memHeapUsage.getInit()));
  memoryMap.put("heapMax", String.valueOf(memHeapUsage.getMax()));
  memoryMap.put("heapCommit", String.valueOf(memHeapUsage.getCommitted()));
  memoryMap.put("heapUsed", String.valueOf(memHeapUsage.getUsed()));
  memoryMap.put("nonHeapInit", String.valueOf(nonHeapUsage.getInit()));
  memoryMap.put("nonHeapMax", String.valueOf(nonHeapUsage.getMax()));
  memoryMap.put("nonHeapCommit", String.valueOf(nonHeapUsage.getCommitted()));
  memoryMap.put("nonHeapUsed", String.valueOf(nonHeapUsage.getUsed()));
  return memoryMap.toString();
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:25,代碼來源:MetricsServoRegistry.java

示例5: sense

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
@Override
public void sense(final MetricRecorder.Context metricContext)
{
    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();

    int current = mxBean.getThreadCount();
    int peak = mxBean.getPeakThreadCount();

    if (peak > historicalPeak) {
        historicalPeak = peak;
    }

    metricContext.record(THREADS, current, Unit.NONE);
    metricContext.record(PERIODIC_PEAK_THREADS, peak, Unit.NONE);
    metricContext.record(PEAK_THREADS, historicalPeak, Unit.NONE);

    mxBean.resetPeakThreadCount();
}
 
開發者ID:awslabs,項目名稱:swage,代碼行數:19,代碼來源:ThreadSensor.java

示例6: testReset

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
@Test
public void testReset() {
    System.out.println("reset");
    ThreadMXBean tbean = ManagementFactory.getThreadMXBean();
    addStacktrace(tbean.getThreadInfo(tbean.getAllThreadIds(), Integer.MAX_VALUE), System.nanoTime());
    addStacktrace(tbean.getThreadInfo(tbean.getAllThreadIds(), Integer.MAX_VALUE), System.nanoTime());

    instance.reset();
    assertTrue(instance.methodInfos.size()-1 == 0);
    assertTrue(instance.threadIds.size() == 0);
    assertTrue(instance.threadNames.size() == 0);
    assertEquals(-1L, instance.currentDumpTimeStamp);
    //assertEquals(-1L, instance.firstDumpTimeStamp);
    assertEquals(0, instance.stackTraceCount);

    try {
        instance.createSnapshot(System.currentTimeMillis());
        fail();
    } catch (CPUResultsSnapshot.NoDataAvailableException ex) {
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:22,代碼來源:StackTraceSnapshotBuilderTest.java

示例7: countTimerThreads

import java.lang.management.ThreadMXBean; //導入依賴的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;
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:17,代碼來源:TestShell.java

示例8: buildDeadlockInfo

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
static String buildDeadlockInfo() {
  ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
  long[] threadIds = threadBean.findMonitorDeadlockedThreads();
  if (threadIds != null && threadIds.length > 0) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter out = new PrintWriter(stringWriter);
    
    ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
    for (ThreadInfo ti : infos) {
      printThreadInfo(ti, out);
      printLockInfo(ti.getLockedSynchronizers(), out);
      out.println();
    }
    
    out.close();
    return stringWriter.toString();
  } else {
    return null;
  }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:21,代碼來源:TimedOutTestsListener.java

示例9: countThreads

import java.lang.management.ThreadMXBean; //導入依賴的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;
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:21,代碼來源:TestRpcBase.java

示例10: 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;
    }
 
開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:24,代碼來源:ThreadingManager.java

示例11: 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;
    }


}
 
開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:19,代碼來源:ThreadingManager.java

示例12: bindTo

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
@Override
public void bindTo(MeterRegistry registry) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();

    Gauge.builder("jvm.threads.peak", threadBean, ThreadMXBean::getPeakThreadCount)
        .tags(tags)
        .description("The peak live thread count since the Java virtual machine started or peak was reset")
        .register(registry);

    Gauge.builder("jvm.threads.daemon", threadBean, ThreadMXBean::getDaemonThreadCount)
        .tags(tags)
        .description("The current number of live daemon threads")
        .register(registry);

    Gauge.builder("jvm.threads.live", threadBean, ThreadMXBean::getThreadCount)
        .tags(tags)
        .description("The current number of live threads including both daemon and non-daemon threads")
        .register(registry);
}
 
開發者ID:micrometer-metrics,項目名稱:micrometer,代碼行數:20,代碼來源:JvmThreadMetrics.java

示例13: 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;
}
 
開發者ID:Axway,項目名稱:ats-framework,代碼行數:21,代碼來源:ThreadUtils.java

示例14: getAllThreads

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
/**
 * @return a list of all threads started in the JVM.
 */
public List<Thread> getAllThreads() {

    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    // get the number of all live threads
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads;

    do {
        nAlloc *= 2; // increase the size since more threads may have been created
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true); // get all active threads from this thread group
    } while (n == nAlloc); // stop if all active threads are enumerated

    List<Thread> res = new ArrayList<Thread>();
    for (Thread th : threads) {
        res.add(th);
    }

    return res;
}
 
開發者ID:Axway,項目名稱:ats-framework,代碼行數:26,代碼來源:ThreadUtils.java

示例15: start

import java.lang.management.ThreadMXBean; //導入依賴的package包/類
public static void start() {
    if (started) throw new IllegalStateException("Already Started.");
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    ThreadMXBean thread = ManagementFactory.getThreadMXBean();
    Runtime r = Runtime.getRuntime();
    int mb = 0x100000;

    lastSystemTime = System.nanoTime();
    lastProcessCpuTime = calculateProcessCpuTime(os);

    Async.task("AsyncInfoMonitorThread", () -> {
        threadCount = thread.getThreadCount();
        availableProcessors = r.availableProcessors();
        freeMemory = Runtime.getRuntime().freeMemory() / mb;
        maxMemory = Runtime.getRuntime().maxMemory() / mb;
        totalMemory = Runtime.getRuntime().totalMemory() / mb;
        cpuUsage = calculateCpuUsage(os);
        vpsCPUUsage = getVpsCPUUsage(os);
        vpsFreeMemory = calculateVPSFreeMemory(os);
        vpsMaxMemory = calculateVPSMaxMemory(os);
        vpsUsedMemory = vpsMaxMemory - vpsFreeMemory;
    }, 1);
    started = true;
}
 
開發者ID:natanbc,項目名稱:GabrielBot,代碼行數:25,代碼來源:AsyncInfoMonitor.java


注:本文中的java.lang.management.ThreadMXBean類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。