This class walks a path, and for each file that it hits, it throws a thread into a ThreadPoolExecutor which collects the objects being given to it into a HashMap. I have another thread that monitors the HashMap and when that Map has 5,000 elements in it, it gets dumped to a MySQL database and then the records that were recorded are purged from the HashMap and everything continues like that.
However, once the walker gets to where it has hit over 2 million files, the actual purging of the HashMap has lagged to where it's almost a million records behind so I would like to be able to pause the folder walking until the data dumps have caught up, then resume ... rinse repeat ...
Is it possible to pause this class after it starts going? OR, is there any way to slow it down possibly?
public class WalkFilePaths implements Runnable{
public WalkFilePaths(Path rootPath, ThreadPoolExecutor executor) {
this.rootPath = rootPath;
this.executor = executor;
}
private final Path rootPath;
private static ThreadPoolExecutor executor;
private static final FileDataManager fileDataManager = new FileDataManager();
@Override public void run() {
try {
FolderWalker folderWalker = new FolderWalker();
Files.walkFileTree(rootPath,folderWalker);
}
catch (IOException e) {e.printStackTrace();}
}
public static class FolderWalker extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
if(attrs.isRegularFile()) {
executor.execute(fileDataManager.addFileMap(new FileDataModel(path.toFile(), attrs.creationTime().toInstant(), attrs.lastAccessTime().toInstant())));
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
}
}