上一篇文章中 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