I am trying to understand safe publication in case of Effectively Immutable classes. For my class I cannot come up with a scenario in which it would be thread unsafe. Do I need to add some other safe gaurd?
CLARIFICATION: Container elements are thread safe
public class Container<E> {
private LinkedList<E> data;
public Container() {
this.data = new LinkedList<E>();
}
public Container(final Container<E> other) {
this.data = new LinkedList<E>(other.data);
}
public Container<E> add(E e) {
Container<E> other_cont= new Container<E>(this);
other_cont.data.add(e);
return other_cont;
}
public Container<E> remove() {
Container<E> other_cont= new Container<E>(this);
other_cont.data.remove(0);
return other_cont;
}
public E peek() {
if(this.data.isEmpty())
throw new NoSuchElementException("No element to peek at");
return this.data.get(0);
}
public int size() {
return this.data.size();
}
}