Package java.lang.management

Examples of java.lang.management.ThreadMXBean


   private static String INDENT = "    ";

   public static String threadDump() {
      StringBuilder threadDump = new StringBuilder();
      ThreadMXBean threadMx = ManagementFactory.getThreadMXBean();
      if (threadMx.isObjectMonitorUsageSupported() && threadMx.isSynchronizerUsageSupported()) {
         // Print lock info if, and only if, both object monitor usage and synchronizer usage are supported.
          dumpThreadInfo(threadDump, true, threadMx);
      } else {
         dumpThreadInfo(threadDump, false, threadMx);
      }
View Full Code Here


   private static String INDENT = "    ";

   public static String threadDump() {
      StringBuilder threadDump = new StringBuilder();
      ThreadMXBean threadMx = ManagementFactory.getThreadMXBean();
      if (threadMx.isObjectMonitorUsageSupported() && threadMx.isSynchronizerUsageSupported()) {
         // Print lock info if, and only if, both object monitor usage and synchronizer usage are supported.
          dumpThreadInfo(threadDump, true, threadMx);
      } else {
         dumpThreadInfo(threadDump, false, threadMx);
      }
View Full Code Here

    @Test
    public void testThreadingMXBean() throws IOException {
        DescribedResource describedResource = basicResourceTest("threading", null);

        ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
        boolean syncSupported = describedResource.resource.get(PlatformMBeanConstants.SYNCHRONIZER_USAGE_SUPPORTED).asBoolean();
        Assert.assertEquals(mbean.isSynchronizerUsageSupported(), syncSupported);
        boolean monitorSupported = describedResource.resource.get(PlatformMBeanConstants.OBJECT_MONITOR_USAGE_SUPPORTED).asBoolean();
        Assert.assertEquals(mbean.isObjectMonitorUsageSupported(), monitorSupported);
        boolean threadContentionSupported = describedResource.resource.get(PlatformMBeanConstants.THREAD_CONTENTION_MONITORING_SUPPORTED).asBoolean();
        Assert.assertEquals(mbean.isThreadContentionMonitoringSupported(), threadContentionSupported);
        boolean threadContentionEnabled = describedResource.resource.get(PlatformMBeanConstants.THREAD_CONTENTION_MONITORING_ENABLED).asBoolean();
        Assert.assertEquals(mbean.isThreadContentionMonitoringEnabled(), threadContentionEnabled);
        boolean threadCPUSupported = describedResource.resource.get(PlatformMBeanConstants.THREAD_CPU_TIME_SUPPORTED).asBoolean();
        Assert.assertEquals(mbean.isThreadCpuTimeSupported(), threadCPUSupported);
        boolean threadCPUEnabled = describedResource.resource.get(PlatformMBeanConstants.THREAD_CPU_TIME_ENABLED).asBoolean();
        Assert.assertEquals(mbean.isThreadCpuTimeSupported(), threadCPUEnabled);
        boolean currentThreadPUSupported = describedResource.resource.get(PlatformMBeanConstants.CURRENT_THREAD_CPU_TIME_SUPPORTED).asBoolean();
        Assert.assertEquals(mbean.isCurrentThreadCpuTimeSupported(), currentThreadPUSupported);

        ModelNode op = getOperation("reset-peak-thread-count", "threading", null);
        Assert.assertFalse(executeOp(op, false).isDefined());

        op = getOperation("find-deadlocked-threads", "threading", null);
        ModelNode result = executeOp(op, !syncSupported);
        if (syncSupported && result.isDefined()) {
            Assert.assertEquals(ModelType.LIST, result.getType());
        }
        op = getOperation("find-monitor-deadlocked-threads", "threading", null);
        result = executeOp(op, !monitorSupported);
        if (monitorSupported && result.isDefined()) {
            Assert.assertEquals(ModelType.LIST, result.getType());
        }

        op = getOperation("dump-all-threads", "threading", null);
        op.get("locked-monitors").set(false);
        op.get("locked-synchronizers").set(false);
        result = executeOp(op, false);
        Assert.assertEquals(ModelType.LIST, result.getType());
        long mainThreadId = findMainThread(result);

        op = getOperation("dump-all-threads", "threading", null);
        op.get("locked-monitors").set(true);
        op.get("locked-synchronizers").set(false);
        result = executeOp(op, !monitorSupported);
        if (monitorSupported) {
            Assert.assertEquals(ModelType.LIST, result.getType());
        }

        op = getOperation("dump-all-threads", "threading", null);
        op.get("locked-monitors").set(false);
        op.get("locked-synchronizers").set(true);
        result = executeOp(op, !syncSupported);
        if (syncSupported) {
            Assert.assertEquals(ModelType.LIST, result.getType());
        }

        op = getOperation("dump-all-threads", "threading", null);
        op.get("locked-monitors").set(true);
        op.get("locked-synchronizers").set(true);
        boolean canDump = syncSupported && monitorSupported;
        result = executeOp(op, !canDump);
        if (canDump) {
            Assert.assertEquals(ModelType.LIST, result.getType());
        }

        op = getOperation("get-thread-info", "threading", null);
        op.get("id").set(mainThreadId);
        result = executeOp(op, false);
        Assert.assertTrue(result.isDefined());
        Assert.assertEquals("main", result.get("thread-name").asString());
        Assert.assertEquals(mainThreadId, result.get("thread-id").asLong());
        List<ModelNode> list = result.get("stack-trace").asList();
        Assert.assertEquals(0, list.size());

        op = getOperation("get-thread-info", "threading", null);
        op.get("id").set(mainThreadId);
        op.get("max-depth").set(2);
        result = executeOp(op, false);
        Assert.assertTrue(result.isDefined());
        Assert.assertEquals("main", result.get("thread-name").asString());
        Assert.assertEquals(mainThreadId, result.get("thread-id").asLong());
        list = result.get("stack-trace").asList();
        Assert.assertEquals(2, list.size());

        op = getOperation("get-thread-infos", "threading", null);
        op.get("ids").add(mainThreadId);
        result = executeOp(op, false);
        Assert.assertEquals(ModelType.LIST, result.getType());
        List<ModelNode> threads = result.asList();
        Assert.assertEquals(1, threads.size());
        ModelNode thread = threads.get(0);
        Assert.assertEquals("main", thread.get("thread-name").asString());
        Assert.assertEquals(mainThreadId, thread.get("thread-id").asLong());
        list = thread.get("stack-trace").asList();
        Assert.assertEquals(0, list.size());

        op = getOperation("get-thread-infos", "threading", null);
        op.get("ids").add(mainThreadId);
        op.get("max-depth").set(2);
        result = executeOp(op, false);
        Assert.assertEquals(ModelType.LIST, result.getType());
        threads = result.asList();
        Assert.assertEquals(1, threads.size());
        thread = threads.get(0);
        Assert.assertEquals("main", thread.get("thread-name").asString());
        Assert.assertEquals(mainThreadId, thread.get("thread-id").asLong());
        list = thread.get("stack-trace").asList();
        Assert.assertEquals(2, list.size());

        op = getOperation("get-thread-infos", "threading", null);
        op.get("ids").add(mainThreadId);
        op.get("locked-monitors").set(true);
        op.get("locked-synchronizers").set(true);
        result = executeOp(op, !canDump);
        if (canDump) {
            Assert.assertEquals(ModelType.LIST, result.getType());
            threads = result.asList();
            Assert.assertEquals(1, threads.size());
            thread = threads.get(0);
            Assert.assertEquals("main", thread.get("thread-name").asString());
            Assert.assertEquals(mainThreadId, thread.get("thread-id").asLong());
            list = thread.get("stack-trace").asList();
            Assert.assertTrue(list.size() > 1);
        }

        op = getOperation("get-thread-cpu-time", "threading", null);
        op.get("id").set(mainThreadId);
        result = executeOp(op, !threadCPUSupported);
        Assert.assertEquals(ModelType.LONG, result.getType());
        if (!threadCPUEnabled) {
            Assert.assertEquals(-1L, result.asLong());
        }

        op = getOperation("get-thread-user-time", "threading", null);
        op.get("id").set(mainThreadId);
        result = executeOp(op, !threadCPUSupported);
        Assert.assertEquals(ModelType.LONG, result.getType());
        if (!threadCPUEnabled) {
            Assert.assertEquals(-1L, result.asLong());
        }

        op = getOperation("write-attribute", "threading", null);
        op.get("name").set("thread-cpu-time-enabled");
        op.get("value").set(!threadCPUEnabled);
        executeOp(op, false);
        Assert.assertEquals(mbean.isThreadCpuTimeEnabled(), !threadCPUEnabled);
        mbean.setThreadCpuTimeEnabled(threadCPUEnabled); // restore

        op = getOperation("write-attribute", "threading", null);
        op.get("name").set("thread-contention-monitoring-enabled");
        op.get("value").set(!threadContentionEnabled);
        executeOp(op, false);
        Assert.assertEquals(mbean.isThreadContentionMonitoringEnabled(), !threadContentionEnabled);
        mbean.setThreadContentionMonitoringEnabled(threadContentionEnabled); // restore
    }
View Full Code Here

        }
        return s.toString();
    }

    public static ThreadInfo[] findDeadlockedThreads() {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        if (threadMXBean.isSynchronizerUsageSupported()) {
            long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
            if (deadlockedThreads == null || deadlockedThreads.length == 0) {
                return null;
            }
            return threadMXBean.getThreadInfo(deadlockedThreads, true, true);
        } else {
            long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
            return getThreadInfos(threadMXBean, monitorDeadlockedThreads);
        }
    }
View Full Code Here

            return getThreadInfos(threadMXBean, monitorDeadlockedThreads);
        }
    }

    public static ThreadInfo[] getAllThreads() {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        if (threadMXBean.isObjectMonitorUsageSupported()
                && threadMXBean.isSynchronizerUsageSupported()) {
            return threadMXBean.dumpAllThreads(true, true);
        }
        long[] allThreadIds = threadMXBean.getAllThreadIds();
        return getThreadInfos(threadMXBean, allThreadIds);
    }
View Full Code Here

        createMemState(memberState, proxyObjects);
    }

    private void createRuntimeProps(MemberStateImpl memberState) {
        Runtime runtime = Runtime.getRuntime();
        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
        ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean();
        MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemory = memoryMxBean.getHeapMemoryUsage();
        MemoryUsage nonHeapMemory = memoryMxBean.getNonHeapMemoryUsage();
        Map<String, Long> map = new HashMap<String, Long>();
        map.put("runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue());
        map.put("date.startTime", runtimeMxBean.getStartTime());
        map.put("seconds.upTime", runtimeMxBean.getUptime());
        map.put("memory.maxMemory", runtime.maxMemory());
        map.put("memory.freeMemory", runtime.freeMemory());
        map.put("memory.totalMemory", runtime.totalMemory());
        map.put("memory.heapMemoryMax", heapMemory.getMax());
        map.put("memory.heapMemoryUsed", heapMemory.getUsed());
        map.put("memory.nonHeapMemoryMax", nonHeapMemory.getMax());
        map.put("memory.nonHeapMemoryUsed", nonHeapMemory.getUsed());
        map.put("runtime.totalLoadedClassCount", clMxBean.getTotalLoadedClassCount());
        map.put("runtime.loadedClassCount", Integer.valueOf(clMxBean.getLoadedClassCount()).longValue());
        map.put("runtime.unloadedClassCount", clMxBean.getUnloadedClassCount());
        map.put("runtime.totalStartedThreadCount", threadMxBean.getTotalStartedThreadCount());
        map.put("runtime.threadCount", Integer.valueOf(threadMxBean.getThreadCount()).longValue());
        map.put("runtime.peakThreadCount", Integer.valueOf(threadMxBean.getPeakThreadCount()).longValue());
        map.put("runtime.daemonThreadCount", Integer.valueOf(threadMxBean.getDaemonThreadCount()).longValue());

        OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
        map.put("osMemory.freePhysicalMemory", get(osMxBean, "getFreePhysicalMemorySize", 0L));
        map.put("osMemory.committedVirtualMemory", get(osMxBean, "getCommittedVirtualMemorySize", 0L));
        map.put("osMemory.totalPhysicalMemory", get(osMxBean, "getTotalPhysicalMemorySize", 0L));
View Full Code Here

        /*
         * Save resource usage, since it will not be available once the
         * thread has exited.
         */
        //ONEJAVA/*
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        totalCpuTime = threadBean.getThreadCpuTime(getId());
        totalUserTime = threadBean.getThreadUserTime(getId());
        //ONEJAVA*/

        if (Thread.currentThread() == this) {
            return;
        }
View Full Code Here

   private static String INDENT = "    ";

   public static String threadDump() {
      StringBuilder threadDump = new StringBuilder();
      ThreadMXBean threadMx = ManagementFactory.getThreadMXBean();
      if (threadMx.isObjectMonitorUsageSupported() && threadMx.isSynchronizerUsageSupported()) {
         // Print lock info if, and only if, both object monitor usage and synchronizer usage are supported.
          dumpThreadInfo(threadDump, true, threadMx);
      } else {
         dumpThreadInfo(threadDump, false, threadMx);
      }
View Full Code Here

        gcCount = count;
        gcTimeMillis = timeMillis;
    }
   
    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;
View Full Code Here

        future.await();

        c1.set(future.getSession());
        c1.get().write("start");

        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

        while (!messageCount.await(100, TimeUnit.MILLISECONDS)) {
            long[] threads = threadMXBean.findMonitorDeadlockedThreads();

            if (null != threads) {
                StringBuffer sb = new StringBuffer(256);
                ThreadInfo[] infos = threadMXBean.getThreadInfo(threads, Integer.MAX_VALUE);

                for (ThreadInfo info : infos) {
                    sb.append(info.getThreadName())
                            .append(" blocked on ")
                            .append(info.getLockName())
View Full Code Here

TOP

Related Classes of java.lang.management.ThreadMXBean

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.