我一直在为此扯头发。我的程序应该在使用简单的双哈希算法搜索哈希表时测量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;
}
DoubleHashThread.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
}
}