classpath
[Top][All Lists]
Advanced

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

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


From: Eric Blake
Subject: Re: java.util.Hashtable.size()/isEmpty()
Date: Fri, 15 Feb 2002 08:28:13 -0700

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.  For thread safety reasons, if you have a
synchronized object, EVERY public access point to information on that
object must be synchronized.

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).
> 

-- 
This signature intentionally left boring.

Eric Blake             address@hidden
  BYU student, free software programmer



reply via email to

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