gpt4 book ai didi

java - 与 Java System.Time 混淆

转载 作者:行者123 更新时间:2023-11-29 08:06:14 25 4
gpt4 key购买 nike

预先感谢您的帮助。我正在开发一个基于 Java 的工具,它正在执行一些数据库工作。我有一个非常简单的问题。由于某种原因,报告的完成任务的时间不正确。

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());
}

该程序报告说,对于我运行的案例,它的实际工作时间大约是实际工作时间的两倍。

谢谢

最佳答案

System.nanoTime() 以纳秒为单位返回时间值。 Time() 将一个以毫秒为单位的值作为参数。这会使您的时间值(value)降低 10^-6 倍。

关于java - 与 Java System.Time 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11005470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com