This discussion is archived
7 Replies Latest reply: Dec 22, 2006 8:30 AM by 807607 RSS

Timing a process in a Thread

807607 Newbie
Currently Being Moderated
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

                      public void 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)
  • 1. Re: Timing a process in a Thread
    807607 Newbie
    Currently Being Moderated
    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?
  • 2. Re: Timing a process in a Thread
    807607 Newbie
    Currently Being Moderated
    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
     }
    }
  • 3. Re: Timing a process in a Thread
    807607 Newbie
    Currently Being Moderated
    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.
  • 4. Re: Timing a process in a Thread
    807607 Newbie
    Currently Being Moderated
    Idea so simple...
    Brain... trying so hard to resist it...
  • 5. Re: Timing a process in a Thread
    doremifasollatido Journeyer
    Currently Being Moderated
    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?
  • 6. Re: Timing a process in a Thread
    807607 Newbie
    Currently Being Moderated
    Tested it on my computer, except I substituted in a Thread.sleep() in place of your video analysis stuff. Worked fine for me.
  • 7. Re: Timing a process in a Thread
    807607 Newbie
    Currently Being Moderated
    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.