private static final ExecutorService ES = Executors.newWorkStealingPool();
public Future<List<String>> isThisSafe() {
List<String> a = new ArrayList<>();
a.add("a");
List<String> b = new ArrayList<>();
b.add("b");
return ES.submit(() -> {
a.addAll(b);
return a;
});
}
I'll say that thread X is the one that calls isThisSafe(). Thread Y runs the Callable submitted to the ExecutorService.
Right now, I think this works.
✓ The two ArrayLists are never modified by X after being published to Y
✓ ArrayList 'a' is modified by thread Y, but X does not hold a reference to 'a'.. the returned reference to 'a' would be considered 'new' and so X will pull this from MM.
But is there a problem with my second point? Maybe I misunderstand the memory model.