本文整理汇总了Java中java.lang.management.ThreadMXBean.getCurrentThreadCpuTime方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadMXBean.getCurrentThreadCpuTime方法的具体用法?Java ThreadMXBean.getCurrentThreadCpuTime怎么用?Java ThreadMXBean.getCurrentThreadCpuTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.management.ThreadMXBean
的用法示例。
在下文中一共展示了ThreadMXBean.getCurrentThreadCpuTime方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCpuTime
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/** Get CPU time in nanoseconds. */
private long getCpuTime( ) {
ThreadMXBean bean = null;
try {
bean = ManagementFactory.getThreadMXBean( );
} catch (Error e) {
// not supported (happens in the by means of IKVM converted version (.net)
return 0L;
}
return bean.isCurrentThreadCpuTimeSupported( ) ?
bean.getCurrentThreadCpuTime( ) : 0L;
}
示例2: thePathOfFifteenThousandJumps
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
@Test
public void thePathOfFifteenThousandJumps() {
byte[] bytecode = new byte[15000 * 6 + 3];
int k = 0;
while (k < 15000 * 6) {
int target = k + 6;
bytecode[k++] = 0x5b; // JUMPDEST
bytecode[k++] = 0x62; // PUSH3
bytecode[k++] = (byte)(target >> 16);
bytecode[k++] = (byte)(target >> 8);
bytecode[k++] = (byte)(target & 0xff);
bytecode[k++] = 0x56; // JUMP
}
bytecode[k++] = 0x5b; // JUMPDEST
bytecode[k++] = 0x60; // PUSH1
bytecode[k++] = 0x01; // 1
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long initialMemory = runtime.totalMemory() - runtime.freeMemory();
long initialTime = thread.getCurrentThreadCpuTime();
testCode(bytecode, 15000 * 3 + 2, "0000000000000000000000000000000000000000000000000000000000000001");
long finalTime = thread.getCurrentThreadCpuTime();
long finalMemory = runtime.totalMemory() - runtime.freeMemory();
System.out.println(String.format("Execution Time %s nanoseconds", finalTime - initialTime));
System.out.println(String.format("Delta memory %s", finalMemory - initialMemory));
}
示例3: spin
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* Actively spins the current thread for at least a given number of milliseconds in such a way
* that timers for the current thread keep ticking over.
*
* @return the number of milliseconds actually spent spinning which is guaranteed to be >=
* {@code ms}
*/
private static long spin(long ms) {
ThreadMXBean threadMXBean = Management.getThreadMXBean();
long start = threadMXBean.getCurrentThreadCpuTime();
do {
long durationMS = (threadMXBean.getCurrentThreadCpuTime() - start) / 1000;
if (durationMS >= ms) {
return durationMS;
}
} while (true);
}