A bit confused about the usage of ConcurrentHashSets in DTOs. This DTO is accessed by many threads at a time.
First Case
public class LogDTO {
private Set<String> person = ConcurrentHashMap.newKeySet();
public void setPerson(Set<String> person) {
this.person = person;
}
public Set<String> getPerson() {
return this.person;
}
}
Does this give Thread Safety?
Second Case
public class LogDTO {
private volatile Set<String> person;
public void setPerson(Set<String> person) {
this.person = person;
}
public Set<String> getPerson() {
return this.person;
}
}
or do I have to use AtomicReference?
Third Case
public class LogDTO {
private AtomicReference<Set<String>> ref = new AtAtomicReference<>();
public void setPerson(Set<String> person) {
this.ref.set(person);
}
public Set<String> getPerson() {
return this.ref.get();
}
}
If to populate an entirely new HashSet then assigning it to the existing variable, then is it better to use volatile?