简体   繁体   中英

CompletableFuture.runAsync(() ->… with a non-final variable

I have this code that works fine:

String name = "Oscar";
CompletableFuture.runAsync(() -> doX(name));

Now I need to add some logic to the name variable:

String name = "Oscar";
if (x){
  name = "Tiger";
}
CompletableFuture.runAsync(() -> doX(name));

But now the compiler complains about Variable used in lambda expression should be final or effectively final

I understand from posts like this one that the name must be final or effectively final, but I wonder if there is a way to write the code differently in order to enable the logic on the name variable

The lambda expression needs a final variable. If the initialization code is complicated enough then define a new final variable.

final String fName = name;
CompletableFuture.runAsync(() -> doX(fName));

You can use Conditional Operator , something like this:

        boolean condition = true;
        String name = condition ? "Tiger" : "Oscar";
        CompletableFuture.runAsync(() -> System.out.println(name));

Or using if statements:

 boolean condition = true;
    final String name;
    if(condition) {
        name = "Tiger";
    }else {
        name = "Oscar";
    }
    CompletableFuture.runAsync(() -> System.out.println(name));

Try to decouple resolver logic from the main flow:

public static void somewhere() {
    // Variable even may be explicitly final
    final String name = resolve(true);
    CompletableFuture.runAsync(() -> doX(name));
}

public String resolve(boolean condition) {
    if (condition)
        return "NameX";

    return "NameY";

    // Or:
    // return condition ? "NameX" : "NameY";
}

The advantage of the approach that you may add more complex conditions and can change this logic later without touching the original flow.

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