Last active
June 22, 2023 09:27
-
-
Save ryan-beckett/f298ab6fe84f3fb8025aa4cb28b8c793 to your computer and use it in GitHub Desktop.
Example of how to parallelize file tree walk and parse.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.nio.file.FileVisitResult; | |
import java.nio.file.FileVisitor; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.SimpleFileVisitor; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.RecursiveAction; | |
import com.github.javaparser.JavaParser; | |
@SuppressWarnings("serial") | |
public class AsynchronousParse extends RecursiveAction { | |
private static final String SRC_DIR_PATH = ""; | |
private final Path dir; | |
private static int numFilesParsed; | |
public AsynchronousParse(Path dir) { | |
this.dir = dir; | |
} | |
@Override | |
protected void compute() { | |
final List<AsynchronousParse> walks = new ArrayList<>(); | |
try { | |
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { | |
@Override | |
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { | |
if (!dir.equals(AsynchronousParse.this.dir)) { | |
AsynchronousParse w = new AsynchronousParse(dir); | |
w.fork(); | |
walks.add(w); | |
return FileVisitResult.SKIP_SUBTREE; | |
} else { | |
return FileVisitResult.CONTINUE; | |
} | |
} | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
if (file.toString().endsWith(".java")) { | |
numFilesParsed++; | |
JavaParser.parse(new FileInputStream(file.toFile())); | |
} | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
for (AsynchronousParse w : walks) { | |
w.join(); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
AsynchronousParse w = new AsynchronousParse(Paths.get(SRC_DIR_PATH).toRealPath()); | |
ForkJoinPool p = new ForkJoinPool(); | |
long startTime = System.nanoTime(); | |
p.invoke(w); | |
double elapsedTime = (System.nanoTime() - startTime) / 1000000; | |
System.out.println("Compilation of " + numFilesParsed + " files took " + elapsedTime + " milliseconds."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's from? https://github.com/javaparser/javaparser/blob/cfc1bcdf1fbd596ac11cbf14be565ffdee8903a5/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java#L568