我一直在为此拔头发。我的程序应该测量在搜索使用简单双哈希算法的哈希表时的 CPU 时间。但是,ThreadMXBean 不断返回-1。任何帮助表示赞赏。
“主要”的片段
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
//CREATE A THREAD
DoubleHashThread doubleThread =
new DoubleHashThread("doublethread"); //CREATE DOUBLE HASHING THREAD
doubleThread.setDaemon(true);
doubleThread.start();
getCpuTime(doubleThread);
doubleThread.run(hashTable,data); //RUN SEARCH FUNCTION
getCpuTime(doubleThread);
GetCpuTime 方法
public static long getCpuTime(DoubleHashThread dt)
{
ThreadMXBean bean = ManagementFactory.getThreadMXBean(); //GET THREAD BEAN
long cpuTime = 0;
if (bean.isThreadCpuTimeSupported()) //if grabbing it is supported, continue
{
System.out.println("CPU time for " + dt.getId() + " " + dt.getName() +
" is " + bean.getThreadCpuTime(dt.getId()));
}
return cpuTime;
}
双哈希线程.java
import doubleHash.DoubleHash;
public class DoubleHashThread extends Thread
{
public DoubleHashThread(String s)
{
this.setName(s);
}
//RUNS SEARCH FUNCTION
public void run(String[] hashTable, String data)
{
DoubleHash.searchTable(hashTable,data); //run Double Hash's search
}
}