1:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43: import ;
44:
45: import ;
46:
47:
58: public final class ThreadMXBeanImpl
59: extends BeanImpl
60: implements ThreadMXBean
61: {
62:
63:
66: private static final String CURRENT_THREAD_TIME_SUPPORT =
67: "gnu.java.lang.management.CurrentThreadTimeSupport";
68:
69:
72: private static final String THREAD_TIME_SUPPORT =
73: "gnu.java.lang.management.ThreadTimeSupport";
74:
75:
78: private static final String CONTENTION_SUPPORT =
79: "gnu.java.lang.management.ThreadContentionSupport";
80:
81:
84: private static final String TIME_ENABLED =
85: "gnu.java.lang.management.ThreadTimeInitallyEnabled";
86:
87:
90: private boolean timeEnabled;
91:
92:
95: private boolean contentionEnabled;
96:
97:
107: public ThreadMXBeanImpl()
108: throws NotCompliantMBeanException
109: {
110: super(ThreadMXBean.class);
111: timeEnabled = Boolean.parseBoolean(SystemProperties.getProperty(TIME_ENABLED));
112: contentionEnabled = false;
113: }
114:
115: public long[] findMonitorDeadlockedThreads()
116: {
117: checkMonitorPermissions();
118: return VMThreadMXBeanImpl.findMonitorDeadlockedThreads();
119: }
120:
121: public long[] getAllThreadIds()
122: {
123: checkMonitorPermissions();
124: return VMThreadMXBeanImpl.getAllThreadIds();
125: }
126:
127: public long getCurrentThreadCpuTime()
128: {
129: if (!isCurrentThreadCpuTimeSupported())
130: throw new UnsupportedOperationException("Current thread CPU " +
131: "time not supported.");
132: if (!timeEnabled)
133: return -1;
134: return VMThreadMXBeanImpl.getCurrentThreadCpuTime();
135: }
136:
137: public long getCurrentThreadUserTime()
138: {
139: if (!isCurrentThreadCpuTimeSupported())
140: throw new UnsupportedOperationException("Current thread user " +
141: "time not supported.");
142: if (!timeEnabled)
143: return -1;
144: return VMThreadMXBeanImpl.getCurrentThreadUserTime();
145: }
146:
147: public int getDaemonThreadCount()
148: {
149: return VMThreadMXBeanImpl.getDaemonThreadCount();
150: }
151:
152: public int getPeakThreadCount()
153: {
154: return VMThreadMXBeanImpl.getPeakThreadCount();
155: }
156:
157: public int getThreadCount()
158: {
159: return VMThreadMXBeanImpl.getThreadCount();
160: }
161:
162: public long getThreadCpuTime(long id)
163: {
164: if (!isThreadCpuTimeSupported())
165: throw new UnsupportedOperationException("Thread CPU time not " +
166: "supported.");
167: if (id <= 0)
168: throw new IllegalArgumentException("Invalid thread id: " + id);
169: if (!timeEnabled)
170: return -1;
171: return VMThreadMXBeanImpl.getThreadCpuTime(id);
172: }
173:
174: public ThreadInfo getThreadInfo(long id)
175: {
176: return getThreadInfo(id, 0);
177: }
178:
179: public ThreadInfo[] getThreadInfo(long[] ids)
180: {
181: return getThreadInfo(ids, 0);
182: }
183:
184: public ThreadInfo getThreadInfo(long id, int maxDepth)
185: {
186: checkMonitorPermissions();
187: if (id <= 0)
188: throw new IllegalArgumentException("Invalid thread id: " + id);
189: if (maxDepth < 0)
190: throw new IllegalArgumentException("Invalid depth: " + maxDepth);
191: return VMThreadMXBeanImpl.getThreadInfoForId(id, maxDepth);
192: }
193:
194: public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth)
195: {
196: checkMonitorPermissions();
197: if (maxDepth < 0)
198: throw new IllegalArgumentException("Invalid depth: " + maxDepth);
199: ThreadInfo[] infos = new ThreadInfo[ids.length];
200: for (int a = 0; a < ids.length; ++a)
201: {
202: if (ids[a] <= 0)
203: throw new IllegalArgumentException("Invalid thread id " + a +
204: ": " + ids[a]);
205: infos[a] = VMThreadMXBeanImpl.getThreadInfoForId(ids[a], maxDepth);
206: }
207: return infos;
208: }
209:
210: public long getThreadUserTime(long id)
211: {
212: if (!isThreadCpuTimeSupported())
213: throw new UnsupportedOperationException("Thread user time not " +
214: "supported.");
215: if (id <= 0)
216: throw new IllegalArgumentException("Invalid thread id: " + id);
217: if (!timeEnabled)
218: return -1;
219: return VMThreadMXBeanImpl.getThreadUserTime(id);
220: }
221:
222: public long getTotalStartedThreadCount()
223: {
224: return VMThreadMXBeanImpl.getTotalStartedThreadCount();
225: }
226:
227: public boolean isCurrentThreadCpuTimeSupported()
228: {
229: if (isThreadCpuTimeSupported())
230: return true;
231: return SystemProperties.getProperty(CURRENT_THREAD_TIME_SUPPORT) != null;
232: }
233:
234: public boolean isThreadContentionMonitoringEnabled()
235: {
236: if (isThreadContentionMonitoringSupported())
237: return contentionEnabled;
238: else
239: throw new UnsupportedOperationException("Contention monitoring " +
240: "not supported.");
241: }
242:
243: public boolean isThreadContentionMonitoringSupported()
244: {
245: return SystemProperties.getProperty(CONTENTION_SUPPORT) != null;
246: }
247:
248: public boolean isThreadCpuTimeEnabled()
249: {
250: if (isThreadCpuTimeSupported() ||
251: isCurrentThreadCpuTimeSupported())
252: return timeEnabled;
253: else
254: throw new UnsupportedOperationException("Thread time not " +
255: "supported.");
256: }
257:
258: public boolean isThreadCpuTimeSupported()
259: {
260: return SystemProperties.getProperty(THREAD_TIME_SUPPORT) != null;
261: }
262:
263: public void resetPeakThreadCount()
264: {
265: checkControlPermissions();
266: VMThreadMXBeanImpl.resetPeakThreadCount();
267: }
268:
269: public void setThreadContentionMonitoringEnabled(boolean enable)
270: {
271: checkControlPermissions();
272: if (isThreadContentionMonitoringSupported())
273: contentionEnabled = enable;
274: else
275: throw new UnsupportedOperationException("Contention monitoring " +
276: "not supported.");
277: }
278:
279: public void setThreadCpuTimeEnabled(boolean enable)
280: {
281: checkControlPermissions();
282: if (isThreadCpuTimeSupported() ||
283: isCurrentThreadCpuTimeSupported())
284: timeEnabled = enable;
285: else
286: throw new UnsupportedOperationException("Thread time not " +
287: "supported.");
288: }
289:
290: }