本文整理匯總了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);
}