To calculate ideal ThreadPool size in my project I have to calculate CPU time and Wait time for a thread. So I have made a dummy project something like this:
class ThreadClass extends Thread
{
public static Long total;
public void run()
{
try
{
Long start=System.currentTimeMillis();
System.out.println("Starting at "+start);
//Thread.sleep(1000);
System.out.println("thread is running...");
Long end=System.currentTimeMillis();
System.out.println("Ending at "+end);
total=end-start;
System.out.println("Total "+total);
}
catch (Exception e){}
}
}
public class Main3 {
public static void main(String[] args) {
ThreadClass t=new ThreadClass();
t.start();
Long tid=t.getId();//currentThread() still showing same
ThreadMXBean threadMXBean= ManagementFactory.getThreadMXBean();
long cpuTime = threadMXBean.getThreadCpuTime(tid)/1000000;
System.out.println("CPU time calculated in MAIN "+cpuTime);
}
}
The Value I am getting : Starting at 1662975991279 thread is running... Ending at 1662975991319 Total 40 CPU time calculated in MAIN 222
Now my question is isn't the "Total" value is wait time + cpu time? Or is it the wait time only? And for this simple code why CPU time is 222 MS and how come this is higher than "Total"??
Am I calculating correctly the CPU time and Wait time?