Skip to content

Instantly share code, notes, and snippets.

@babanin
Created July 14, 2017 16:07
Show Gist options
  • Save babanin/f5e0b94171fe21d3cbd01d1b9a0e3c41 to your computer and use it in GitHub Desktop.
Save babanin/f5e0b94171fe21d3cbd01d1b9a0e3c41 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author Ivan Babanin ([email protected])
*/
public class ProbeFileInParallel {
public static void main(String[] args) throws IOException {
final List<Path> paths = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("."), "*.{docx}")) {
for (Path path : stream) {
paths.add(path);
}
}
System.out.println("Found following files: ");
for (Path path : paths) {
System.out.println("\t" + path);
}
System.out.println("Probing...");
final ExecutorService executorService = Executors.newFixedThreadPool(20);
for (int i = 0; i < 20; i++) {
executorService.submit(new Runnable() {
@Override
public void run() {
for (Path path : paths) {
try {
System.out.println(Files.probeContentType(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment