有如下代码,想问下这种情况下会有线程安全问题吗,我回调方法里去赋值this.result1和this.result2,然后在最后去读者两个字段,读和写不是同一线程不用同步吗?
public void newInit() throws ExecutionException, InterruptedException { CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> { return 1L; }).thenAccept(result -> { this.result1 = result; }); CompletableFuture cf2 = CompletableFuture.supplyAsync(() -> { return 2L; }).thenAccept(result -> { this.result2 = result; }); CompletableFuture.allOf(cf1, cf2).get(); System.out.println(this.result1) }
精慕HU
12345678_0001
相关分类