1 /* TestThread.java -- Tests the thread bean.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 package gnu.classpath.examples.management;
23 import java.lang.management.ManagementFactory;
24 import java.lang.management.ThreadInfo;
25 import java.lang.management.ThreadMXBean;
27 import java.util.Arrays;
29 public class TestThread
32 public static void main(String[] args)
34 ThreadMXBean bean = ManagementFactory.getThreadMXBean();
35 System.out.println("Bean: " + bean);
36 System.out.println("Monitor deadlocked threads: " + bean.findMonitorDeadlockedThreads());
37 long[] ids = bean.getAllThreadIds();
38 System.out.println("Live thread ids: " + Arrays.toString(ids));
39 boolean currentTimeMonitoring = bean.isCurrentThreadCpuTimeSupported();
40 System.out.println("Current thread CPU time monitoring supported: " + currentTimeMonitoring);
41 if (currentTimeMonitoring)
43 boolean timeEnabled = bean.isThreadCpuTimeEnabled();
44 System.out.println("Is time monitoring enabled... " +
45 (timeEnabled ? "yes" : "no"));
48 System.out.println("Enabling...");
49 bean.setThreadCpuTimeEnabled(true);
50 timeEnabled = bean.isThreadCpuTimeEnabled();
51 System.out.println("Should now be enabled... " +
52 (timeEnabled ? "yes" : "no"));
56 System.out.println("Current thread CPU time: "
57 + bean.getCurrentThreadCpuTime()
59 System.out.println("Current thread user time: "
60 + bean.getCurrentThreadUserTime()
64 System.out.println("Daemon thread count: " + bean.getDaemonThreadCount());
65 System.out.println("Peak thread count: " + bean.getPeakThreadCount());
66 System.out.println("Resetting...");
67 bean.resetPeakThreadCount();
68 System.out.println("Peak thread count: " + bean.getPeakThreadCount());
69 System.out.println("Thread count: " + bean.getThreadCount());
70 boolean timeMonitoring = bean.isThreadCpuTimeSupported();
71 System.out.println("Thread CPU time monitoring supported: " + timeMonitoring);
74 for (int a = 0; a < ids.length; ++a)
76 System.out.println("Thread " + a
78 + bean.getThreadCpuTime(ids[a]) + "ns");
79 System.out.println("Thread "
81 + bean.getThreadUserTime(ids[a]) + "ns");
84 System.out.println("Current thread info: "
85 + bean.getThreadInfo(Thread.currentThread().getId()));
86 System.out.println("All thread info: " + Arrays.toString(bean.getThreadInfo(ids)));
87 System.out.println("Total started threads: " + bean.getTotalStartedThreadCount());
88 boolean contentionMonitoring = bean.isThreadContentionMonitoringSupported();
89 System.out.println("Thread contention monitoring supported: " + contentionMonitoring);
90 if (contentionMonitoring)
92 boolean contentionEnabled = bean.isThreadContentionMonitoringEnabled();
93 System.out.println("Thread contention monitoring shouldn't be enabled... " +
94 (contentionEnabled ? "but it is" : "true"));
95 if (!contentionEnabled)
97 System.out.println("Enabling...");
98 bean.setThreadContentionMonitoringEnabled(true);
99 contentionEnabled = bean.isThreadContentionMonitoringEnabled();
100 System.out.println("Should now be enabled... " +
101 (contentionEnabled ? "it is" : "nope"));
103 if (contentionEnabled)
105 ThreadInfo[] info = bean.getThreadInfo(ids);
106 for (int a = 0; a < info.length; ++a)
108 System.out.println("Blocked time for thread "
109 + info[a].getThreadId() + ": "
110 + info[a].getBlockedTime() + "ms");
111 System.out.println("Waited time for thread "
112 + info[a].getThreadId() + ": "
113 + info[a].getWaitedTime() + "ms");