View Single Post
  #3 (permalink)  
Old 01-11-2008, 12:19 AM
Deathbob Deathbob is offline
Member
 
Join Date: Jan 2008
Posts: 1
Deathbob is on a distinguished road
There is a way using pure java. It may take some adapting to fit your needs. The monitoring could be placed on it's own thread and have a handle to the thread you wish to monitor.

cpuperc in this case holds the aprox. cpu usage %. It is fairly accurate. You could average several to get a better idea.

Code:
//import java.lang.management.*; ThreadMXBean TMB = ManagementFactory.getThreadMXBean(); long time = new Date().getTime() * 1000000; long cput = 0; double cpuperc = -1; //Begin loop. if( TMB.isThreadCpuTimeSupported() ) { if(new Date().getTime() * 1000000 - time > 1000000000) //Reset once per second { time = new Date().getTime() * 1000000; cput = TMB.getCurrentThreadCpuTime(); } if(!TMB.isThreadCpuTimeEnabled()) { TMB.setThreadCpuTimeEnabled(true); } if(new Date().getTime() * 1000000 - time != 0) cpuperc = (TMB.getCurrentThreadCpuTime() - cput) / (new Date().getTime() * 1000000.0 - time) * 100.0; } else { cpuperc = -2; } //Do cpu intensive stuff //End Loop
Reply With Quote