classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: java.util.Hashtable.size()/isEmpty()


From: Bryce McKinlay
Subject: Re: java.util.Hashtable.size()/isEmpty()
Date: Sat, 16 Feb 2002 10:43:15 +1300
User-agent: Mozilla/5.0 (X11; U; Linux ppc; en-US; rv:0.9.7) Gecko/20011221

Eric Blake wrote:

No - even accessing a simple field must be synchronized, for this
reason:

Suppose thread A is in the middle of calling clear() when thread B calls
size().  Notice in clear() that the size is not changed until after all
entries have been set to null, meaning that B will read a larger size
than what is actually there.


So? Even if size() is synchronized there is nothing to stop another thread calling clear() after size()'s lock is released but before the first thread does anything with the value returned by size().

The only way to avoid issues like this is for the caller to put a big synchronized block around all its calls into the hashtable.

regards

Bryce.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]