how to measure running time of a thread?

hello,

is it possible to measure the running time of a specific thread, instead of measuring the running time (via System.currentTimeMillis ()) of the whole program?

What i need, is the time of a thread on the cpu.

What i don't need is something like this:

...

Thread t =new Thread ();

begin = System.currentTimeMillis ();

t.start ();

t.join ();

System.out.println (System.currentTimeMillis () - begin);

...

and this:

...

publicvoid run ()

{

long end;

long runningTimeOfThread;

long begin;

begin = System.currentTimeMillis ();

//do some time consuming work

end = System.currentTimeMillis ();

runningTimeOfThread = end - begin;

}

...

what i need is something like this (or something equivalent):

...

publicvoid run ()

{

long end;

long runningTimeOfThread;

long begin;

begin = measureRunningTimeOfThisThread ();

//do some time consuming work

end = measureRunningTimeOfThisThread ();

runningTimeOfThread = end - begin;

}

tnx in advance

mamurdian

[1704 byte] By [mamurdiana] at [2007-10-3 11:04:24]
# 1
In Java 5 onwards, check out the Monitoring & Management extensions and JVMTI interface. It may not be supported to the same degree on all platforms as OS support is needed for accurate per-thread CPU time measurement.
davidholmesa at 2007-7-15 13:26:51 > top of Java-index,Core,Core APIs...
# 2
long cpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
Maris_Orbidansa at 2007-7-15 13:26:51 > top of Java-index,Core,Core APIs...
# 3

public void run ()

{

long end;

long runningTimeOfThread;

long begin;

begin = measureRunningTimeOfThisThread ();

while(!theEndOfWork){

System.out.println("Take a cup of coffee");

}

end = measureRunningTimeOfThisThread ();

runningTimeOfThread = end - begin;

}

Mithua at 2007-7-15 13:26:51 > top of Java-index,Core,Core APIs...
# 4

> >

> public void run ()

> {

> long end;

> long runningTimeOfThread;

> long begin;

>

> begin = measureRunningTimeOfThisThread ();

>

> while(!theEndOfWork){

> System.out.println("Take a cup of coffee");

>}

> = measureRunningTimeOfThisThread ();

>

> runningTimeOfThread = end - begin;

> }

>

>

i'm not sure if i understand your post correctly, but if it is was your intention to tell me that i can use a while loop instead of my initial java comment, i think you have misunderstood me.

measureRunningTimeOfThisThread () is not a "real" method. It is only placeholder that i used to emphasize my requirement. What i need is a replacement for measureRunningTimeOfThisThread ().

greets

mamurdian

mamurdiana at 2007-7-15 13:26:51 > top of Java-index,Core,Core APIs...
# 5

Perhaps the code snippet should something like...

public void run ()

{

long runningTimeOfThread;

while(!theEndOfWork){

System.out.println("Take a cup of coffee");

}

runningTimeOfThread = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();

}

" runningTimeOfThread" indicating the value used by the current thread..

AmitBaida at 2007-7-15 13:26:51 > top of Java-index,Core,Core APIs...