本文整理汇总了Java中java.lang.management.ThreadMXBean.isThreadCpuTimeSupported方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadMXBean.isThreadCpuTimeSupported方法的具体用法?Java ThreadMXBean.isThreadCpuTimeSupported怎么用?Java ThreadMXBean.isThreadCpuTimeSupported使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.management.ThreadMXBean
的用法示例。
在下文中一共展示了ThreadMXBean.isThreadCpuTimeSupported方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getJvmThreadInstCpuTimeNs
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* Getter for the "JvmThreadInstCpuTimeNs" variable.
*/
public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException {
long l = 0;
final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();
try {
if (tmb.isThreadCpuTimeSupported()) {
l = tmb.getThreadCpuTime(info.getThreadId());
log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l);
//Cpu time measurement is disabled or the id is not valid.
if(l == -1) l = 0;
}
} catch (UnsatisfiedLinkError e) {
// XXX Revisit: catch TO BE EVENTUALLY REMOVED
log.debug("getJvmThreadInstCpuTimeNs",
"Operation not supported: " + e);
}
return new Long(l);
}
示例2: main
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
long value = mbean.getInternalThreadCount();
if (value < MIN_VALUE_FOR_PASS || value > MAX_VALUE_FOR_PASS) {
throw new RuntimeException("Internal thread count " +
"illegal value: " + value + " " +
"(MIN = " + MIN_VALUE_FOR_PASS + "; " +
"MAX = " + MAX_VALUE_FOR_PASS + ")");
}
System.out.println("Internal Thread Count = " + value);
ThreadMXBean thread =
ManagementFactory.getThreadMXBean();
if (!thread.isThreadCpuTimeSupported()) {
System.out.println("Thread Cpu Time is not supported.");
return;
}
while(!testCPUTime()) {
Thread.sleep(100);
}
}
示例3: getJvmThreadCpuTimeMonitoring
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* Getter for the "JvmThreadCpuTimeMonitoring" variable.
*/
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
throws SnmpStatusException {
ThreadMXBean mbean = getThreadMXBean();
if(!mbean.isThreadCpuTimeSupported()) {
log.debug("getJvmThreadCpuTimeMonitoring",
"Unsupported ThreadCpuTimeMonitoring");
return JvmThreadCpuTimeMonitoringUnsupported;
}
try {
if(mbean.isThreadCpuTimeEnabled()) {
log.debug("getJvmThreadCpuTimeMonitoring",
"Enabled ThreadCpuTimeMonitoring");
return JvmThreadCpuTimeMonitoringEnabled;
} else {
log.debug("getJvmThreadCpuTimeMonitoring",
"Disabled ThreadCpuTimeMonitoring");
return JvmThreadCpuTimeMonitoringDisabled;
}
}catch(UnsupportedOperationException e) {
log.debug("getJvmThreadCpuTimeMonitoring",
"Newly unsupported ThreadCpuTimeMonitoring");
return JvmThreadCpuTimeMonitoringUnsupported;
}
}
示例4: checkJvmThreadCpuTimeMonitoring
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* Checker for the "JvmThreadCpuTimeMonitoring" variable.
*/
public void checkJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring
x)
throws SnmpStatusException {
//Can't be set externaly to unsupported state.
if(JvmThreadCpuTimeMonitoringUnsupported.intValue() == x.intValue()) {
log.debug("checkJvmThreadCpuTimeMonitoring",
"Try to set to illegal unsupported value");
throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
}
if ((JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue()) ||
(JvmThreadCpuTimeMonitoringDisabled.intValue() == x.intValue())) {
// The value is a valid value. But is the feature supported?
ThreadMXBean mbean = getThreadMXBean();
if(mbean.isThreadCpuTimeSupported()) return;
// Not supported.
log.debug("checkJvmThreadCpuTimeMonitoring",
"Unsupported operation, can't set state");
throw new
SnmpStatusException(SnmpDefinitions.snmpRspInconsistentValue);
}
// Unknown value.
log.debug("checkJvmThreadCpuTimeMonitoring",
"unknown enum value ");
throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
}
示例5: main
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
public static void main(String argv[]) {
ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
int cnt = 0;
long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE};
if (mbean.isThreadCpuTimeSupported()) {
for (int i = 0; i < idArr.length; i++) {
try {
mbean.getThreadCpuTime(idArr[i]);
System.out.println("Test failed. IllegalArgumentException" +
" expected for ID = " + idArr[i]);
} catch (IllegalArgumentException iae) {
cnt++;
}
}
if (cnt != idArr.length) {
throw new RuntimeException("Unexpected number of " +
"IllegalArgumentException = " + cnt +
" expected = " + idArr.length);
}
// CPU time for a non-existence thread
long time = mbean.getThreadCpuTime(999999);
if (time < 0 && time != -1) {
throw new RuntimeException("Cpu time for thread 999999" +
" is invalid = " + time + " expected to be -1.");
}
}
System.out.println("Test passed.");
}
示例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();
}
}