import java.io.File;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ParallelGroupFileSearch {
public static void searchFiles(File file, String fileName, Result parallelResult) {
ConcurrentLinkedQueue<File> directories = new ConcurrentLinkedQueue<>();
File[] contents = file.listFiles();
for (File content : contents) {
if (content.isDirectory()) {
directories.add(content);
}
}
int numThreads = Runtime.getRuntime().availableProcessors();
Thread[] threads = new Thread[numThreads];
ParallelGroupFileTask[] tasks = new ParallelGroupFileTask[numThreads];
for (int i = 0; i < numThreads; i++) {
tasks[i] = new ParallelGroupFileTask(fileName, parallelResult, directories);
threads[i] = new Thread(tasks[i]);
threads[i].start();
}
boolean finish = false;
int numFinished = 0;
while (!finish) {
numFinished = 0;
for (int i = 0; i < threads.length; i++) {
if (threads[i].getState() == Thread.State.TERMINATED) {
numFinished++;
if (tasks[i].getFound()) {
finish = true;
}
}
}
if (numFinished == threads.length) {
finish = true;
}
if (numFinished != threads.length) {
for (Thread thread : threads) {
thread.interrupt();
}
}
}
}
}
import java.io.File;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ParallelGroupFileTask implements Runnable{
private final String fileName;
private final ConcurrentLinkedQueue<File> directories;
private final Result parallelResult;
private boolean found;
public ParallelGroupFileTask(String fileName, Result parallelResult,
ConcurrentLinkedQueue<File> directories) {
this.fileName = fileName;
this.parallelResult = parallelResult;
this.directories = directories;
this.found = false;
}
@Override
public void run() {
while (directories.size() > 0) {
File file = directories.poll();
try {
processDirectory(file, fileName, parallelResult);
if (found) {
System.out.printf("%s has found the file%n", Thread.currentThread().getName());
System.out.printf("Parallel Search: Path: %s%n", parallelResult.getPath());
return;
}
} catch (InterruptedException ie) {
System.out.printf("%s has been interrupted%n", Thread.currentThread().getName());
}
}
}
private void processDirectory(File file, String fileName, Result parallelResult) throws InterruptedException {
File[] contents;
contents = file.listFiles();
if (contents == null || contents.length == 0) {
return;
}
for (File content : contents) {
if (content.isDirectory()) {
processDirectory(content, fileName, parallelResult);
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
if (found) {
return;
}
} else {
processFile(content, fileName, parallelResult);
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
if (found) {
return;
}
}
}
}
private void processFile(File content, String fileName, Result parallelResult) {
if (content.getName().equals(fileName)) {
parallelResult.setPath(content.getAbsolutePath());
this.found = true;
}
}
public boolean getFound() {
return found;
}
}
public class Result {
private String absolutePath;
private boolean found = false;
public void setPath(String absolutePath) {
this.absolutePath = absolutePath;
}
public Object getPath() {
return this.absolutePath;
}
public void setFound(boolean found) {
this.found = found;
}
public boolean isFound() {
return this.found;
}
}