jdk/src/share/classes/java/util/Hashtable.java
changeset 8394 49215a09f282
parent 7816 55a18147b4bf
child 9035 1255eb81cc2f
--- a/jdk/src/share/classes/java/util/Hashtable.java	Mon Feb 14 10:48:46 2011 -0800
+++ b/jdk/src/share/classes/java/util/Hashtable.java	Mon Feb 14 11:00:02 2011 -0800
@@ -845,24 +845,36 @@
      *             for each key-value mapping represented by the Hashtable
      *             The key-value mappings are emitted in no particular order.
      */
-    private synchronized void writeObject(java.io.ObjectOutputStream s)
-        throws IOException
-    {
-        // Write out the length, threshold, loadfactor
-        s.defaultWriteObject();
+    private void writeObject(java.io.ObjectOutputStream s)
+            throws IOException {
+        Entry<Object, Object> entryStack = null;
+
+        synchronized (this) {
+            // Write out the length, threshold, loadfactor
+            s.defaultWriteObject();
+
+            // Write out length, count of elements
+            s.writeInt(table.length);
+            s.writeInt(count);
 
-        // Write out length, count of elements and then the key/value objects
-        s.writeInt(table.length);
-        s.writeInt(count);
-        for (int index = table.length-1; index >= 0; index--) {
-            Entry entry = table[index];
+            // Stack copies of the entries in the table
+            for (int index = 0; index < table.length; index++) {
+                Entry entry = table[index];
 
-            while (entry != null) {
-                s.writeObject(entry.key);
-                s.writeObject(entry.value);
-                entry = entry.next;
+                while (entry != null) {
+                    entryStack =
+                        new Entry<>(0, entry.key, entry.value, entryStack);
+                    entry = entry.next;
+                }
             }
         }
+
+        // Write out the key/value objects from the stacked entries
+        while (entryStack != null) {
+            s.writeObject(entryStack.key);
+            s.writeObject(entryStack.value);
+            entryStack = entryStack.next;
+        }
     }
 
     /**