Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitoring thread performance/overriding static methods.

Status
Not open for further replies.

agentBrown

Programmer
Apr 14, 2002
1
NL
For an application I'm writing, I would like to keep tabs on how much CPU power the various threads are taking up. However, I do not see a suitable method in either java.lang.Thread or java.lang.ThreadGroup that gives such information.

Is there a way to find CPU load for each Thread?

If not, I was thinking of overriding Thread.sleep and such to time threads myself. When I start a thread, make a timestamp. When the thread exits, make another timestamp. That would work except to be 'fair' to the threadcode I need to filter out the time that the thread was in 'sleep' mode, either through Thread.sleep, Thread.yield, or one of the blocking java.io or java.net functions. One way of doing this is timestamping right before and right after a sleep/yield (and perhaps I can find out a way to timestamp time spent in blocking IO/net code).

However, will this kind of thing work: (pseudoish)

Code:
 public class StampingThread
  extends Thread {
    public static void yield() {
      long timestamp = currentTime;
      Thread.yield();
      long inactiveTime = currentTime - timestamp;
    }
 }

wil this method be called instead of the one in java.lang.Thread, when I create a StampingThread, and use Thread.yield() in the code that will be run by the StampingThread?
 
Static methods in Java are not inherited and therefore may not be overridden. Other languages have similar constraints such as C++ and C#, still others such as Smalltalk allow polymorphism to extend to Class-level methods.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top