I had to crash course a bit on Java threading due to time constraints, so pardon me if this question seems ignorant. The main idea is that I'm trying to get the CPU time for the DoubleHash's search function, but for some reason, I've been getting CPU time as zero even though the thread seems to run and terminate fine. Any help is appreciated.
DoubleHashThread.java
import doubleHash.DoubleHash;
public class DoubleHashThread extends Thread
{
public String[] hashTable= new String[DoubleHash.TABLE_SIZE];
public String studentId;
public DoubleHashThread(String s)
{
this.setName(s);
}
//OVERRIDE RUN TO RUN SEARCH FUNCTION
@Override
public void run()
{
int a = 0;
for(int i = 0; i < 1; i++)
{
a++;
DoubleHash.searchTable(hashTable, studentId);
DoubleHash.getCpuTime(this);
System.out.println("inside thread, is alive: " + this.isAlive());
}
return;
}
Snippet of Main
//CREATE A THREAD AND ADD TO LIST
threadList.add(new DoubleHashThread("Double Hash Thread"));
threadList.get(0).setDaemon(true);
//UPDATE THREAD HASH TABLE
threadList.get(0).hashTable = hashTable;
//GET USER INPUT
sc.nextLine(); //clear buffer
System.out.println("Please Enter the Student ID ");
data=sc.nextLine();
//UPDATE WANTED STUDENT ID
threadList.get(0).studentId = data;
//START THREAD EXECUTION
threadList.get(0).start();
//TERMINATE THREAD AND REMOVE FROM LIST
try
{
threadList.get(0).join();
threadList.remove(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
getCpuTime
public static long getCpuTime(DoubleHashThread dt)
{
ThreadMXBean bean = ManagementFactory.getThreadMXBean(); //GET THREAD BEAN
long cpuTime = -1000;
if (bean.isThreadCpuTimeSupported())
{
System.out.println("in get cpu method, status: " + dt.getState());
System.out.println("CPU time for ID: " + dt.getId() +
" THREAD NAME: " + dt.getName() + " is " + bean.getThreadCpuTime(dt.getId()));
cpuTime = bean.getThreadCpuTime(dt.getId());
}
return cpuTime;
}
Update 1 Funnily enough, the first thread CPU time it returns me is not 0, but subsequent ones are always 0.