简体   繁体   English

为执行程序服务定义文件路径

[英]Defining a file path for an executor service

im writing a program which will take user input on a file ie:我正在编写一个程序,它将接受用户对文件的输入,即:

            if (option == 1) {
                System.out.print("Enter text file path: ");
                bookFile = scanner.next();
            }

After defining the file path it will go into the executor:定义文件路径后,它将进入执行程序:

        // Parse book file concurrently
        executor.execute(() -> {
            try {
                parseBookFile(bookFile, wordIndex);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

The issue im coming across is the bookfile is underlined stating: Local variable bookFile defined in an enclosing scope must be final or effectively final我遇到的问题是 bookfile 有下划线说明:在封闭范围内定义的局部变量 bookFile 必须是最终的或有效的最终的

And i cant seem to find a way around this since the file path cant be final before defining it.而且我似乎找不到解决这个问题的方法,因为文件路径在定义它之前不能是最终的。 The only thing that worked is defining the file path withing the executor itself but this really messes with the rest of the program唯一有用的是使用执行程序本身定义文件路径,但这确实会扰乱程序的其余部分

I tried to define the file path witin the executor but it doesnt work out since the next block of code is defining more file paths but the user must choose to do so if that makes sense.我试图在执行程序中定义文件路径,但它没有成功,因为下一个代码块正在定义更多文件路径,但如果有意义,用户必须选择这样做。

It looks like the issue you're facing is caused by the fact that the bookFile variable is being used within an anonymous inner class (ie, the execute method's lambda function).看起来您面临的问题是由匿名内部类(即 execute 方法的 lambda 函数)中使用 bookFile 变量这一事实引起的。 In Java, if a local variable is used within an anonymous inner class, it must be marked as final or effectively final.在 Java 中,如果在匿名内部类中使用局部变量,则必须将其标记为 final 或有效的 final。

One way to resolve this issue is to define the bookFile variable as a field of the enclosing class, rather than a local variable within the if block.解决此问题的一种方法是将 bookFile 变量定义为封闭类的一个字段,而不是 if 块中的局部变量。 This way, the variable can be accessed and modified from within the anonymous inner class without any issues.这样,可以从匿名内部类中访问和修改变量而不会出现任何问题。

Alternatively, you could also use a method parameter to pass the value of bookFile to the anonymous inner class, rather than accessing it as a local variable.或者,您也可以使用方法参数将 bookFile 的值传递给匿名内部类,而不是将其作为局部变量访问。 For example:例如:

executor.execute(() -> { try { parseBookFile(bookFile, wordIndex); } catch (Exception e) { e.printStackTrace(); } }); executor.execute(() -> { try { parseBookFile(bookFile, wordIndex); } catch (Exception e) { e.printStackTrace(); } });

I hope this helps.我希望这有帮助。 Let me know if you have any further questions.如果您还有其他问题,请告诉我。

One possible solution, it is to use a class that implement Runnable, in order way to pass the variables as below:一种可能的解决方案是使用实现 Runnable 的类,以便按如下方式传递变量:

        class MyJob implements Runnable{
            private final int wordIndex;
            private final String bookFile;
            public MyJob(String bookFile,int wordIndex){
                this.wordIndex = wordIndex;
                this.bookFile = bookFile;
            }
             public void run(){
                    
                    try {
                        parseBookFile(bookFile, wordIndex);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
             }
        }

So, you can run a thread in this way:所以,你可以这样运行一个线程:

executor.execute( new MyJob(bookFile,wordIndex));

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

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