简体   繁体   English

Java线程代码流

[英]java thread code flow

I'm really puzzled by the following pieces of code : 以下代码片段让我感到非常困惑:

@Component
public class DrawingFileExplorer {

    private final ExecutorService pool = Executors.newFixedThreadPool(10);

        public void explore(File drawingFolder) throws InterruptedException {       
            for(File each : drawingFolder.listFiles(DrawingFileFilter.getInstance())) {
                if(each.isDirectory()) {
                    explore(each);
                } else {
                    //pool.execute(new DrawingFileReviewer(each));
                }
            }
            System.out.println("THIS LINE OF CODE SHOULD BE INVOKED ONCE");

            pool.shutdown();
            pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
        }

}

// Trigger of DrawingFileExplorer
public class DrawingFileExplorerTest {

    private static File baseFolder = new File("C:\\Users\\Jake\\Desktop\\baseFolder\\02. Current Drawings");

    public static void main(String[] args) throws InterruptedException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        DrawingFileExplorer drawingFileExplorer = (DrawingFileExplorer) ctx.getBean("drawingFileExplorer");
        drawingFileExplorer.explore(baseFolder);
    }

}

As of i know the line System.out.println(..) after foreach should be invoked only once. 就我所知,foreach之后的System.out.println(..)行应仅被调用一次。 but the output of the code is following. 但是代码的输出如下。

THIS LINE OF CODE SHOULD BE INVOKED ONCE
THIS LINE OF CODE SHOULD BE INVOKED ONCE

Could anyone explain how it can be invoked twice? 谁能解释一下如何调用它两次? my thread between first output and second line of output are not executed due to the shutdown(). 由于shutdown(),我的第一输出和第二行输出之间的线程未执行。

您的方法是递归的,为什么您不希望一遍又一遍地调用它(每个目录一次)?

There's a recursive call after checking if each is a directory, that is - 检查each目录是否为目录后,将进行递归调用,即-

 if(each.isDirectory()) {
   explore(each); // <-- HERE

So you will get multiple(s) of your line if (and only if) there are sub-directories. 因此,当(且仅当)存在子目录时,您才会获得一行的多个。

Your explore is recursive method, which can be invoked even multiple times based on number of directories. 您的explore是递归方法,根据目录数,它甚至可以多次调用。

for(File each : drawingFolder.listFiles(DrawingFileFilter.getInstance())) {
                if(each.isDirectory()) {
                    explore(each);  // this call is inside for loop and will be invoke recursively.
                } else {
                    //pool.execute(new DrawingFileReviewer(each));
                }
            }

For eg, given directory structure will print the line for 5 times, 例如,给定的目录结构将打印该行5次,

DraginFolder DraginFolder

SubFolder1 SubFolder1

  SubFolder2 

SubFolder3 SubFolder3

SubFolder4 SubFolder4

You have to move 你必须搬家

System.out.println("THIS LINE OF CODE SHOULD BE INVOKED ONCE");
pool.shutdown();
pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);

outside of explore explore之外

When the code executed on a directory only contain files, the above 3 lines code will be executed. 当在目录上执行的代码仅包含文件时,将执行以上三行代码。 And because you shutdown the pool, you will cannot explore all files. 而且由于关闭了池,因此无法浏览所有文件。

@Component
public class DrawingFileExplorer {

    private final ExecutorService pool = Executors.newFixedThreadPool(10);

    public void explore(File drawingFolder) throws InterruptedException {   
        _explore(drawingFolder);

        pool.shutdown();
        pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
    }

    private void _explore(File drawingFolder) {
        for(File each : drawingFolder.listFiles(DrawingFileFilter.getInstance())) {
            if(each.isDirectory()) {
                _explore(each);
            } else {
                pool.execute(new DrawingFileReviewer(each));
            }
        }
    }

}

made another _explorer(..) method to solve it thanks. 提出了另一个_explorer(..)方法来解决它,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM