Timing a process in a Thread

Im not so great with threads and im getting this strange behavior.

Im spawning a new thread for a load process.

I am just marking the start and end time with

System.currentTimeMillis() but im getting very strange results.

Processes that are obviously taking 10 seconds come up as taking .032 seconds or something equally not right.

Here is the code essentially:

// Start of load

long starttime = System.currentTimeMillis();

Program.log("\n\nLoading File...");

try{

Thread loadThread =new Thread(new Loader());

loadThread.start();

}catch(Exception e){

e.printStackTrace();

}

long endtime = System.currentTimeMillis();

// The thread process

publicvoid run(){// this is the thread runnable

long starttime = System.currentTimeMillis();

try{

// blocking load process

long endtime = System.currentTimeMillis();

System.out.println(end-start)

}catch(Exception e){}

}

Specifically the time is gotten with:

(((double)(endtime - starttime))/1000.0)

[1925 byte] By [TuringPesta] at [2007-11-26 13:22:56]
# 1

public void run() {

long startTime = System.currentTimeMillis();

try {

long endTime = System.currentTimeMillis();

System.out.println(endTime - startTime);

} catch(Exception e) { }

}

That isnt your actual method is it?

CaptainMorgan08a at 2007-7-7 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 2

Nope

public void run() {

long startTime = System.currentTimeMillis();

try {

// There is rather long and blocking load process (video anaylsis) here

long endTime = System.currentTimeMillis();

System.out.println("Process took: " + (((double)(endtime - starttime))/1000.0) + " ms");

} catch(Exception e) {

// errors are caught and processed here

}

}

TuringPesta at 2007-7-7 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 3
I would be interested to see where you're calculating the elapsed time. At the point at which you're calculating the e.t. try spitting out the values you're using to make the calculation and see if it yields anything useful.PS.
puckstopper31a at 2007-7-7 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 4
Idea so simple...Brain... trying so hard to resist it...
TuringPesta at 2007-7-7 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 5

If you divide millis by 1000, you are printing "seconds", not "ms". So, are you interpreting your output correctly, even though you label it incorrectly?

Are you printing the time taken in the main thread, too? If so, are you differentiating them (label at beginning of println) so you know which is which in the output?

doremifasollatidoa at 2007-7-7 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 6
Tested it on my computer, except I substituted in a Thread.sleep() in place of your video analysis stuff. Worked fine for me.
CaptainMorgan08a at 2007-7-7 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 7

No the "ms" was just a mistake I typed in that reply (i wasnt copying from code).

Anyway, I found it. Thank you so much!

If someone hadnt pointed out the most obvious thing (the Method of Verbose Outputs, lol) I would have missed out on something important.

Im working with a 3rd party library for video loading.

I was doing

-start time

-load

-end time

-output # frames

and apparently the load process doesnt block - cause the load process doesnt load, lol.

In fact, when i query their Video class for anything (frames, frame rate, etc) for the first time THATS when they do the 30 second loading.

LAME.

thanks again.

TuringPesta at 2007-7-7 17:54:42 > top of Java-index,Java Essentials,Java Programming...