Visibility guarantee with CompletableFuture's thenAccept

233 Views Asked by At

I'm trying to understand if there are any visibility-guarantees provided by CompletableFuture.

Suppose I've a class called SampleClass which is something like the following:

public class SampleClass {
    private String member1;
    private String member2;
    // getters, setters and constructor;
}

And I do something like this:

SampleClass sampleClass = new SampleClass();

CompletableFuture<Void> cf1 = CompletableFuture.supplyAsync(() -> "Hello")
        .thenAccept(sampleClass::setMember1);

CompletableFuture<Void> cf2 = CompletableFuture.supplyAsync(() -> " World")
        .thenAccept(sampleClass::setMember2);

cf1.join();
cf2.join();

// sout(sampleClass);

Now, I want to understand that in the sout statement, can there be a case when one or both the members is/are not initialized?

Basically is there any visibility-guarantee that's provided by CompletableFuture in here? (I'm under the impression that there's no such guarantee provided by CompletableFuture and the above code is broken.)

I've already gone through this question but I think it's not what I need.

0

There are 0 best solutions below