Java 以线程池的方式统计文件夹

上一篇文章中 https://www.zhblog.net/go/java/tutorial/java8-future-task?t=590

是使用多线程统计文件夹,每个文件夹新启一个线程统计。现引入线程池进行管理。

package com.learn.corejava.threading;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.*;

public class FuturePoolMain {

    public static void main(String[] args) {
        String path = "E:\\cspp";
        String keyword = "python";
        ExecutorService pool = Executors.newCachedThreadPool();
        FileMatchCounter fileMatchCounter = new FileMatchCounter(new File(path), keyword, pool);
        Future<Integer> future = pool.submit(fileMatchCounter);
        try {
            System.out.println(future.get() + " matching files.");
            pool.shutdown();

            int largestPoolSize = ((ThreadPoolExecutor)pool).getLargestPoolSize();
            System.out.println("largest pool size = " + largestPoolSize);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class FileMatchCounter implements Callable<Integer> {
    private File directory;
    private String keyword;
    private ExecutorService pool;
    private int count;

    public FileMatchCounter(File directory, String keyword, ExecutorService pool) {
        this.directory = directory;
        this.keyword = keyword;
        this.pool = pool;
    }

    @Override
    public Integer call() throws Exception {
        count = 0;
        File[] files = directory.listFiles();
        List<Future<Integer>> futures = new ArrayList<>();
        for (File file : files) {
            if (file.isDirectory()) {
                FileMatchCounter fileMatchCounter = new FileMatchCounter(file, keyword, pool);
                Future<Integer> future = pool.submit(fileMatchCounter);
                futures.add(future);
            } else {
                if (search(file)) {
                    count ++;
                }
            }
        }

        for (Future<Integer> future : futures) {
            count += future.get();
        }
        return count;
    }

    public boolean search(File file) {
        try(Scanner in = new Scanner(file, "utf-8")) {
            boolean found = false;
            while (!found && in.hasNextLine()) {
                String line = in.nextLine();
                if (line.contains(keyword)) {
                    found = true;
                }
            }
            return found;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}

 

 

原文地址: https://www.zhblog.net/go/java/tutorial/java8-thread-pool-future-task?t=591

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值