ForkJoinPool统计日志文件中的关键词

把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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值