1

Thank you in advance for your help. I am developing a java based tool that is preforming some database work. I have a very simple problem. For some reason the time reported to complete the task is incorrect.

public static void makeDatabaseThreaded() throws IOException, InterruptedException {        
    final long startTime = System.nanoTime();       

    ArrayList<String> tablesMade = new ArrayList<>();
    File rootDirectory = root;
    String[] files = rootDirectory.list();
    double percentDone = 0;
    double numOfTablesMade = 0;
    double numberOfTables = 62.0;
    DatabaseBuilderThread lastThread = null;
    for (int i = 0; i <= files.length - 1; i++) {

        if (!files[i].contains(".csv")) {
            continue;
        }
        File file = new File(rootDirectory + File.separator + files[i]);

        String tableName = getTableNameFromFile(file);
        if (!tablesMade.contains(tableName)) {
            tablesMade.add(tableName);

            DatabaseBuilderThread thread = new DatabaseBuilderThread(i, file);
            lastThread = thread;
            thread.start();
            threadsRunning++;

            numOfTablesMade++;
            percentDone = (int) (100.0 * (numOfTablesMade) / (numberOfTables));

            while (threadsRunning > 10) {
                Thread.sleep(1000);
            }
            System.out.println(percentDone + "% done. Making Table For File: "  + file.getName());
        }
    }

    //Make Sure all threads are done
    lastThread.join();

    final long endTime = System.nanoTime();
    final long duration = endTime - startTime;
    Time time = new Time(duration);
    System.out.println("Done Making The Database. It took " + time.toString());
}

The program reports that it worked about twice as long at it truly did for the cases that I ran.

Thanks

Orlan
  • 541
  • 2
  • 7
  • 15

2 Answers2

4

System.nanoTime() returns time values in nanoseconds. Time() takes a value in milliseconds as a parameter. This would throw your time value off by a factor of 10^-6.

MattS
  • 717
  • 2
  • 7
  • 22
  • Thanks. That was it. For some reason I was looking into the multithreading to see if that caused it. – Orlan Jun 12 '12 at 22:06
1

Time takes milliseconds as a constructor parameter, where as nanoTime() gives you nanoseconds precision, could that be the problem?

discussion here: System.currentTimeMillis vs System.nanoTime

Community
  • 1
  • 1
mindandmedia
  • 6,800
  • 1
  • 24
  • 33