I have this piece of code:
AtomicReference<List<String>> atomicStrings = new AtomicReference<>();
atomicStrings.set(someFunc());
Thread.sleep(10000);
System.out.print(String.join(",", atomicStrings.get()); // will this print a,b,c ?
Where
private List<String> someFunc() {
List<String> list = new ArrayList<>();
new Thread(() -> {
try {
list.add("a");
Thread.sleep(1000);
list.add("b");
Thread.sleep(1000);
list.add("c");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
return list;
}
Of course this is a very poor example but i tried to mimic my real test case here by adding delays. My question here is that since someFunc() returns the array right away and the array elements are populated in a different thread, but the result we get is stored in an AtomicReference and we don't get the value until later, the delay i am adding in the main function is more than the delay the new thread spawned will take. Will my returned array be populated will all the elements?