Reputation:
Here is a snippet from page 72:
@ThreadSafe
public class BetterVector<E> extends Vector<E> {
public synchronized boolean putIfAbsent(E x) {
boolean absent = !contains(x);
if (absent)
add(x);
return absent;
}
}
According to Brian the above class is thread safe. But as you can see that E x
is really a mutable class. What would happen if after evaluating absent
to true
the value of x
changes? Isn't this a violation and might lead to pretty nasty bugs?
Upvotes: 0
Views: 61
Reputation: 11992
Yes you are correct. But that is not an issue with threaded code or concurrency. That's a computer science type problem dealing with references and pointers in general. A reference points to an object. If you pass references around, as Java does, then those references can be used to access the underlying object.
Upvotes: 1