Trying to understand the theory about volatile
.
They say, volatile
provides happens-before, so changes done in thread 1 should be visible in thread 2.
What if we will put volatile for Collection? Will java provide visibility in Thread 2 for changes done in Thread 1 ? And why?
public class Vol {
volatile List<String> list = new ArrayList<>();
// thread 1
public void put(String s) {
list.add(s);
}
// thread 2
public List<String> get(){ // all changes are visible? "Why" for No\Yes answer
return list;
}
}