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             [EMAIL PROTECTED]
  BYU student, free software programmer

_______________________________________________
Classpath mailing list
[EMAIL PROTECTED]
http://mail.gnu.org/mailman/listinfo/classpath

Reply via email to