1

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.

Torben
  • 3,805
  • 26
  • 31
Cheryl
  • 67
  • 12
  • `public static long getCpuTime(DoubleHashThread dt)` - why static? Would that be threadsafe? – Scary Wombat Sep 13 '18 at 06:10
  • see https://stackoverflow.com/questions/13910976/how-to-ensure-thread-safety-of-utility-static-method – Scary Wombat Sep 13 '18 at 06:12
  • I am not familiar with this function, but the Javadoc for it says that it has nanosecond precision, but not necessarily nanosecond accuracy. Is it possible that whatever the thread is doing just doesn't take long enough to register as anything past zero? Like, if the accuracy is 16 milliseconds, and the thread takes 5 milliseconds, I imagine it will say zero. Perhaps try putting in an explicit long delay (like a second) into the thread's work, just to see what happens? – Bob Vesterman Sep 13 '18 at 06:14
  • Furthermore `DoubleHash.getCpuTime(this);` you do not even use the return value – Scary Wombat Sep 13 '18 at 06:14
  • There is also `isThreadCpuTimeEnabled()` which looks like it might bear on this problem (although I think it should return -1 if not). Bob has a good point, I think this could be an issue. Is there some way to independently get the thread time? A debugger for example? Or an OS tool? Can you verify that a particular thread has at least 100 milliseconds of CPU time so we don't have to worry about the timer resolution? – markspace Sep 13 '18 at 06:20
  • @Scary Wombat Thanks for the link, I'll read up on that. I am also aware that I am not using the returned cpuTime, which is intended for future implementations which I have not coded yet. However, I am currently resolving why the println in the getCpuTime method printing it as 0. – Cheryl Sep 13 '18 at 07:09
  • @BobVesterman, thanks for the tip, unfortunately, even after I replaced run() with 1000 iterations of adding an arbitrary number, it still printlns me a cpuTime of zero :( – Cheryl Sep 13 '18 at 07:12
  • @markspace Can confirm that all the threads have CPU time enabled – Cheryl Sep 13 '18 at 07:12
  • I am facing similar issue too. Specifically in windows machine. Same code in Linux returning me non zero value. – Vaibhav Jain Aug 19 '22 at 04:41

0 Answers0