Re: Cannot seem to lock HashMap
byoder@hotmail.com wrote:
You are correct, I posted the wrong version of my code. The "public
void put()..." should have read "public synchronized void put()..." -
so you are correct on this point.
But correct results are not?
No, not really. I DO NOT care if the value returned from get() is "up-
to-date" or not, think of it like "dirty read". My requirements are
very simple...
You seem to be assuming atomic behavior, so that the get result is
either the old or the new value. There is nothing in the API
documentation that promises atomic get.
As far as I can tell, the actual get implementation has real
vulnerabilities, even in a simple single processor situation, without
getting into the complexities of memory order.
Consider the following two lines from the get implementation:
int i = indexFor(hash, table.length);
Entry<K,V> e = table[i];
Suppose there is an interrupt and context switch between the two lines.
Some other thread does a put that causes a rehash with a larger table
size. The hash may no longer belong in bucket i. If get searches the
wrong bucket it will return null regardless of whether the key is
actually present or not.
This could happen with everything else synchronized, as long as get
itself is not synchronized.
Patricia