我有一个使用 java.nio.file.WatchService 寻找新文件的无限轮询循环。在循环内,我修复了线程池执行器服务来同时处理文件。
随着轮询服务持续运行,我如何对一批(例如 10/n 个)文件的处理时间进行基准测试。我能够对可运行类中的每个文件进行计时,但如何获得批处理时间?
最佳答案
这样的事情应该有效:
// inside the listener for the WatchService
final MyTimer t = new MyTimer(); // takes current time, initialized to 0 tasks
for (Change c : allChanges) {
t.incrementTaskCount(); // synchronized
launchConcurrentProcess(c, t);
}
// inside your processor, after processing a change
t.decrementTaskCount(); // also synchronized
// inside MyTimer
public void synchronized decrementTaskCount() {
totalTasks --;
// depending on your benchmarking needs, you can do different things here
// I am printing max time only (= end of last), but min/max/avg may also be nice
if (totalTasks == 0) {
System.err.println("The time spent on this batch was "
+ System.currentTimeMillis() - initialTime);
}
}
关于java - 如何对无限循环 java nio watchservice 程序进行基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775819/