我正在使用java java.nio.file.WatchService监视目录并怀疑内存泄漏。有人可以确认以下代码是否正确,或者会导致内存/线程泄漏吗?我不会在任何地方关闭监视程序。可以是一个问题吗?仅供参考,我正在使用jdk8。
public void run() throws InterruptedException {
while (true) {
logger.info("Checking for file to be created in landing directory, dir: " + landingFolderStr);
WatchKey watchKey = watcher.take();
logger.info("Got file in landing directory, start processing");
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
logger.warn("Watch event might have been lost or discarded, need to investigate.");
} else if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
logger.info("Got new landing file in: " + landingFolder + ", start processing...");
// Ingest data for thoughput and rlr
processHandlerOutput(landingFolder, discardFolder);
}
}
if (!watchKey.reset()) {
logger.error("Exiting since file system events (WatchKey - " + watchKey + ") are no longer acessible.");
break;
}
}