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:05:55 +1300
User-agent: Mozilla/5.0 (X11; U; Linux ppc; en-US; rv:0.9.7) Gecko/20011221

Etienne M. Gagnon wrote:

On Fri, Feb 15, 2002 at 11:19:15AM +0100, Sascha Brawer wrote:

Hello,

the implementations of java.util.Hashtable.size() and
java.util.Hashtable.isEmpty() merely access a single integer member
field.  Since accesses to integer variables are required to be atomic, I
think these two methods don't need to be synchronized. Do people agree?
(Yes, it's a micro-optimization).


I do not agree.  If I remeber correctly, this atomicity is only true
of "volatile" variables.  Otherwise, you have no garantee that the
integer value visible to one thread is the same as the one visible to
another on a multiprocessor system.


You are correct that, without synchronization, the value for size could be seen differently by different CPUs at the same time. However, synchronizing is itself no guarantee of "thread safety" here. Even if it were synchronized the value could have been changed by another thread before getSize() returns, so it makes no difference either way. Hashtable and Vector (and the "synchronized" method modifier for that matter) are so bogus!

regards

Bryce.





reply via email to

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