把Oracle官方的Demo进行了一些修改,修改成了自己想要的
package forkjoin.rgy;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ForkJoinPool;
public class Test {
public static void main(String[] args) throws IOException {
//目标文件夹
String targetFolder = "d:/logs/";
// 目标词
String targetWord = "null";
f1(targetFolder,targetWord);
}
static void f1(String targetFolder, String targetWord) throws IOException{
ForkJoinPool forkJoinPool = new ForkJoinPool();
//把文件夹中的数据加载到内存中,我这个文件夹中就一个日志文件
Folder folder = Folder.fromDirectory(new File(targetFolder));
long startTime = System.currentTimeMillis();
//开始执行fork/join任务
long counts = forkJoinPool.invoke(new FolderSearchTask(folder, targetWord));
long stopTime = System.currentTimeMillis();
long completeTime = stopTime - startTime;
System.out.println(counts + " , fork / join search took " + completeTime + "ms");
}
}
package forkjoin.rgy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Document {
private final List<String> lines;
Document(List<String> lines) {
this.lines = lines;
}
List<String> getLines() {
return this.lines;
}
static Document fromFile(File file) throws IOException {
List<String> lines = new ArrayList<>();
try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
}
return new Document(lines);
}
}
package forkjoin.rgy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Folder {
private final List<Folder> subFolders;
private final List<Document> documents;
Folder(List<Folder> subFolders, List<Document> documents) {
this.subFolders = subFolders;
this.documents = documents;
}
List<Folder> getSubFolders() {
return this.subFolders;
}
List<Document> getDocuments() {
return this.documents;
}
static Folder fromDirectory(File dir) throws IOException {
List<Document> documents = new ArrayList<>();
List<Folder> subFolders = new ArrayList<>();
for (File entry : dir.listFiles()) {
if (entry.isDirectory()) {
subFolders.add(Folder.fromDirectory(entry));
} else {
documents.add(Document.fromFile(entry));
}
}
return new Folder(subFolders, documents);
}
}
package forkjoin.rgy;
import java.util.concurrent.RecursiveTask;
public class DocumentSearchTask extends RecursiveTask<Long> {
private final Document document;
private final String searchedWord;
DocumentSearchTask(Document document, String searchedWord) {
super();
this.document = document;
this.searchedWord = searchedWord;
}
@Override
protected Long compute() {
return occurrencesCount(document, searchedWord);
}
String[] wordsIn(String line) {
return line.trim().split("(\\s|\\p{Punct})+");
}
Long occurrencesCount(Document document, String searchedWord) {
long count = 0;
for (String line : document.getLines()) {
for (String word : wordsIn(line)) {
if (searchedWord.equals(word)) {
count = count + 1;
}
}
}
return count;
}
}
package forkjoin.rgy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;
public class FolderSearchTask extends RecursiveTask<Long> {
private final Folder folder;
private final String searchedWord;
FolderSearchTask(Folder folder, String searchedWord) {
super();
this.folder = folder;
this.searchedWord = searchedWord;
}
//计算方法
@Override
protected Long compute() {
long count = 0L;
List<RecursiveTask<Long>> forks = new ArrayList<>();
//获取文件夹下的子文件夹
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;
}
}