ConcurrentHashMap serialization fix

Peter Levart peter.levart at
Thu Apr 25 07:47:55 PDT 2013


I propose a fix for new segment-less ConcurrentHashMap serialization 
that is found in lambda hg repository:

The fix is 3-fold:

1. If new segment-less CHM is serialized and de-serialization is 
attempted on JDK7, NullPointerException is thrown. That's because 
CHM.Segment nested class is a subclass of ReentrantLock in JDK7 which 
contains a non-transient field "sync" which is initialized to null upon 
de-serialization from new format. The fix adds back "extends 
ReentrantLock" to CHM.Segment to fix the serialization format of new CHM.

2. I think that new CHM's writeObject has a data race. It temporarily 
sets the CHM.segments field to a structure, calls defaultWriteObject and 
then resets the field back to null. If the same instance of CHM is 
concurrently serialized by 2 or more threads the defaultWriteObject can 
see either null or half-initialized segments structure in some thread. 
The fix gets rid of CHM.segments field and uses serialPersistentFields 
array to simulate the field and changes the writeObject to use local 
instance of segments array for populating the stream.

3. The removal of CHM.segments field saves 8 bytes with 64 bit OOPS (no 
difference with 32 bit OOPS unfortunately).

Regards, Peter

More information about the lambda-dev mailing list