@Override protected Long compute() { long count = 0L; List<RecursiveTask<Long>> forks = new LinkedList<>(); for (Folder subFolder : folder.getSubFolders()) { FolderSearchTask task = new FolderSearchTask(subFolder, searchedWord); forks.add(task); task.fork(); } for (Document document : folder.getDocuments()) { DocumentSearchTask task = new DocumentSearchTask(document, searchedWord); forks.add(task); task.fork(); } for (RecursiveTask<Long> task : forks) { count = count + task.join(); } return count; }
Long countOccurrencesOnSingleThread(Folder folder, String searchedWord) { long count = 0; for (Folder subFolder : folder.getSubFolders()) { count = count + countOccurrencesOnSingleThread(subFolder, searchedWord); } for (Document document : folder.getDocuments()) { count = count + occurrencesCount(document, searchedWord); } return count; }
public static void main(String[] args) throws IOException { WordCounter wordCounter = new WordCounter(); Folder folder = Folder.fromDirectory(new File(args[0])); final int repeatCount = Integer.decode(args[2]); long counts; long startTime; long stopTime; long[] singleThreadTimes = new long[repeatCount]; long[] forkedThreadTimes = new long[repeatCount]; for (int i = 0; i < repeatCount; i++) { startTime = System.currentTimeMillis(); counts = wordCounter.countOccurrencesOnSingleThread(folder, args[1]); stopTime = System.currentTimeMillis(); singleThreadTimes[i] = (stopTime - startTime); System.out.println(counts + " , single thread search took " + singleThreadTimes[i] + "ms"); } for (int i = 0; i < repeatCount; i++) { startTime = System.currentTimeMillis(); counts = wordCounter.countOccurrencesInParallel(folder, args[1]); stopTime = System.currentTimeMillis(); forkedThreadTimes[i] = (stopTime - startTime); System.out.println(counts + " , fork / join search took " + forkedThreadTimes[i] + "ms"); } System.out.println("\nCSV Output:\n"); System.out.println("Single thread,Fork/Join"); for (int i = 0; i < repeatCount; i++) { System.out.println(singleThreadTimes[i] + "," + forkedThreadTimes[i]); } System.out.println(); }
static Folder fromDirectory(File dir) throws IOException { List<Document> documents = new LinkedList<>(); List<Folder> subFolders = new LinkedList<>(); for (File entry : dir.listFiles()) { if (entry.isDirectory()) { subFolders.add(Folder.fromDirectory(entry)); } else { documents.add(Document.fromFile(entry)); } } return new Folder(subFolders, documents); }