简体   繁体   中英

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

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). In Java, if a local variable is used within an anonymous inner class, it must be marked as final or effectively 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. 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. For example:

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:

        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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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