0

hey i have this function in my code:

public synchronized void finished()
{
    howManyDone++;
    log.append("Finished creating board "+this.howManyDone+"\n");
    if(howManyDone == boards.length)
        JOptionPane.showMessageDialog(log, "All Boards Were Created.","Attention",JOptionPane.WARNING_MESSAGE);
}

i want to add to the log.append command how much evrey thread ran in sec. i tried to do this one:

public synchronized void finished()
{
    long start = System.nanoTime();
    howManyDone++;
    long end = System.nanoTime();
    long estTime = end - start;
    double seconds = (double)estTime/1000000000;
}

and than print the seconds in each time like this:

log.append("Finished creating board " +this.howManyDone+ " in "+seconds+"\n");

but the numbers i get in the log as the seconds appears like that: 6.00E-7 and so on... what am i doing wrong?

thanks

Sentry
  • 4,102
  • 2
  • 30
  • 38

1 Answers1

0

It won't be possible to get the total run time unless you were able to note the system time when the thread started work. Simply calling it at the start of the finished function won't be enough.

For one solution, you could create a Thread subclass that keeps track of how long it runs, as such:

public class TimedThread extends Thread {

    private long startTime;
    private long endTime;

    public TimedThread(Runnable r) {
        super(r);
    }

    @Override
    public void run() {
        startTime = System.nanoTime();
        super.run();
        endTime = System.nanoTime();
    }

    public long getRunDuration() {
        return endTime - startTime;
    }
}

Then use TimedThreads instead of Threads where you need a timing calculation (assuming you hae control over that section of code).

Mshnik
  • 7,032
  • 1
  • 25
  • 38