土成 2015-12-11 09:59 采纳率: 83.3%
浏览 1397

关于线程池中get()的疑问

如果我的文件夹下共有五个空文件,2个txt文档,那么会产生6个线程,但是为什么在下面代码中count只出现了5次?每个线程都会运行到这段代码的啊。

    for (Future<Integer> futureItem : results) {
                count += futureItem.get();
                        System.out.println("count出现了一次");
            }

运行结果如下:
图片说明
代码如下:

 public class ThreadPoolTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);
        System.out.println("please enter the starting file:");
        String fileName = sc.nextLine();
        System.out.println("please enter what you wang to find:");
        String keybroad = sc.nextLine();

        File file = new File(fileName);
        ExecutorService pool = Executors.newCachedThreadPool();
        MathCounter mathCounter = new MathCounter(file, keybroad,pool);
        Future<Integer> future = pool.submit(mathCounter);
        try {
            System.out.println("包含关键字的文件的个数:"+ future.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

       class MathCounter implements Callable<Integer>{

        File file;
        String keybroad;
        ExecutorService pool;
        List<Future<Integer>> results = new ArrayList<>();

        public MathCounter(File file, String keybroad, ExecutorService pool) {
            // TODO Auto-generated constructor stub
            this.file = file;
            this.keybroad = keybroad;
            this.pool = pool;
        }

        @Override
            public Integer call() throws Exception {
            // TODO Auto-generated method stub
            int count=0;
            File[] files = file.listFiles();
            String string;
            for (File file_search : files) {
                if (file_search.isDirectory()) {
                    MathCounter mathCounter = new MathCounter(file_search, keybroad,pool);
                    Future<Integer> future = pool.submit(mathCounter);
                    results.add(future);
                }else {
                    if (file_search.canRead()) {
                        boolean flag = false;
                        int numberLine = 0;
                        Scanner scanner = new Scanner(file_search);
                        while(scanner.hasNextLine()){
                            numberLine++;
                            string = scanner.nextLine();
                            if (string.contains(keybroad)) {
                                flag = true;
                                System.out.printf("%s: %d, %s\n", file_search.getName(), numberLine, string);
                            }
                        }
                        if (flag == true) {
                            count++;
                        }
                    }
                }//else
            }

            for (Future<Integer> futureItem : results) {
                count += futureItem.get();
                        System.out.println("count出现了一次");
            }

            return count;
        }

    }
  • 写回答

1条回答 默认 最新

  • 落寞红尘 2015-12-11 11:21
    关注

    关于这个多线程的问题,你可以参考这篇文章深入理解线程与进程的概念

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码