ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
(Generate patch)

Comparing jsr166/src/jsr166e/ConcurrentHashMapV8.java (file contents):
Revision 1.25 by dl, Mon Sep 19 12:31:07 2011 UTC vs.
Revision 1.37 by dl, Sun Mar 4 20:34:27 2012 UTC

# Line 20 | Line 20 | import java.util.Enumeration;
20   import java.util.ConcurrentModificationException;
21   import java.util.NoSuchElementException;
22   import java.util.concurrent.ConcurrentMap;
23 + import java.util.concurrent.ThreadLocalRandom;
24   import java.util.concurrent.locks.LockSupport;
25   import java.io.Serializable;
26  
# Line 71 | Line 72 | import java.io.Serializable;
72   * versions of this class, constructors may optionally specify an
73   * expected {@code concurrencyLevel} as an additional hint for
74   * internal sizing.  Note that using many keys with exactly the same
75 < * {@code hashCode{}} is a sure way to slow down performance of any
75 > * {@code hashCode()} is a sure way to slow down performance of any
76   * hash table.
77   *
78   * <p>This class and its views and iterators implement all of the
# Line 98 | Line 99 | public class ConcurrentHashMapV8<K, V>
99      private static final long serialVersionUID = 7249069246763182397L;
100  
101      /**
102 <     * A function computing a mapping from the given key to a value,
103 <     * or {@code null} if there is no mapping. This is a place-holder
103 <     * for an upcoming JDK8 interface.
102 >     * A function computing a mapping from the given key to a value.
103 >     * This is a place-holder for an upcoming JDK8 interface.
104       */
105      public static interface MappingFunction<K, V> {
106          /**
107 <         * Returns a value for the given key, or null if there is no
108 <         * mapping. If this function throws an (unchecked) exception,
109 <         * the exception is rethrown to its caller, and no mapping is
110 <         * recorded.  Because this function is invoked within
111 <         * atomicity control, the computation should be short and
112 <         * simple. The most common usage is to construct a new object
113 <         * serving as an initial mapped value.
107 >         * Returns a non-null value for the given key.
108           *
109           * @param key the (non-null) key
110 <         * @return a value, or null if none
110 >         * @return a non-null value
111           */
112          V map(K key);
113      }
114  
115 +    /**
116 +     * A function computing a new mapping given a key and its current
117 +     * mapped value (or {@code null} if there is no current
118 +     * mapping). This is a place-holder for an upcoming JDK8
119 +     * interface.
120 +     */
121 +    public static interface RemappingFunction<K, V> {
122 +        /**
123 +         * Returns a new value given a key and its current value.
124 +         *
125 +         * @param key the (non-null) key
126 +         * @param value the current value, or null if there is no mapping
127 +         * @return a non-null value
128 +         */
129 +        V remap(K key, V value);
130 +    }
131 +
132      /*
133       * Overview:
134       *
# Line 134 | Line 145 | public class ConcurrentHashMapV8<K, V>
145       * work off Object types. And similarly, so do the internal
146       * methods of auxiliary iterator and view classes.  All public
147       * generic typed methods relay in/out of these internal methods,
148 <     * supplying null-checks and casts as needed.
148 >     * supplying null-checks and casts as needed. This also allows
149 >     * many of the public methods to be factored into a smaller number
150 >     * of internal methods (although sadly not so for the five
151 >     * sprawling variants of put-related operations).
152       *
153       * The table is lazily initialized to a power-of-two size upon the
154       * first insertion.  Each bin in the table contains a list of
155 <     * Nodes (most often, zero or one Node).  Table accesses require
156 <     * volatile/atomic reads, writes, and CASes.  Because there is no
157 <     * other way to arrange this without adding further indirections,
158 <     * we use intrinsics (sun.misc.Unsafe) operations.  The lists of
159 <     * nodes within bins are always accurately traversable under
160 <     * volatile reads, so long as lookups check hash code and
161 <     * non-nullness of value before checking key equality.
155 >     * Nodes (most often, the list has only zero or one Node).  Table
156 >     * accesses require volatile/atomic reads, writes, and CASes.
157 >     * Because there is no other way to arrange this without adding
158 >     * further indirections, we use intrinsics (sun.misc.Unsafe)
159 >     * operations.  The lists of nodes within bins are always
160 >     * accurately traversable under volatile reads, so long as lookups
161 >     * check hash code and non-nullness of value before checking key
162 >     * equality.
163       *
164       * We use the top two bits of Node hash fields for control
165       * purposes -- they are available anyway because of addressing
166       * constraints.  As explained further below, these top bits are
167 <     * usd as follows:
167 >     * used as follows:
168       *  00 - Normal
169       *  01 - Locked
170       *  11 - Locked and may have a thread waiting for lock
# Line 158 | Line 173 | public class ConcurrentHashMapV8<K, V>
173       * The lower 30 bits of each Node's hash field contain a
174       * transformation (for better randomization -- method "spread") of
175       * the key's hash code, except for forwarding nodes, for which the
176 <     * lower bits are zero (and so always have hash field == "MOVED").
176 >     * lower bits are zero (and so always have hash field == MOVED).
177       *
178 <     * Insertion (via put or putIfAbsent) of the first node in an
178 >     * Insertion (via put or its variants) of the first node in an
179       * empty bin is performed by just CASing it to the bin.  This is
180       * by far the most common case for put operations.  Other update
181       * operations (insert, delete, and replace) require locks.  We do
# Line 179 | Line 194 | public class ConcurrentHashMapV8<K, V>
194       * validate that it is still the first node after locking it, and
195       * retry if not. Because new nodes are always appended to lists,
196       * once a node is first in a bin, it remains first until deleted
197 <     * or the bin becomes invalidated.  However, operations that only
198 <     * conditionally update may inspect nodes until the point of
199 <     * update. This is a converse of sorts to the lazy locking
200 <     * technique described by Herlihy & Shavit.
197 >     * or the bin becomes invalidated (upon resizing).  However,
198 >     * operations that only conditionally update may inspect nodes
199 >     * until the point of update. This is a converse of sorts to the
200 >     * lazy locking technique described by Herlihy & Shavit.
201       *
202       * The main disadvantage of per-bin locks is that other update
203       * operations on other nodes in a bin list protected by the same
# Line 266 | Line 281 | public class ConcurrentHashMapV8<K, V>
281       * The element count is maintained using a LongAdder, which avoids
282       * contention on updates but can encounter cache thrashing if read
283       * too frequently during concurrent access. To avoid reading so
284 <     * often, resizing is normally attempted only upon adding to a bin
285 <     * already holding two or more nodes. Under uniform hash
286 <     * distributions, the probability of this occurring at threshold
287 <     * is around 13%, meaning that only about 1 in 8 puts check
288 <     * threshold (and after resizing, many fewer do so). But this
289 <     * approximation has high variance for small table sizes, so we
290 <     * check on any collision for sizes <= 64.
284 >     * often, resizing is attempted either when a bin lock is
285 >     * contended, or upon adding to a bin already holding two or more
286 >     * nodes (checked before adding in the xIfAbsent methods, after
287 >     * adding in others). Under uniform hash distributions, the
288 >     * probability of this occurring at threshold is around 13%,
289 >     * meaning that only about 1 in 8 puts check threshold (and after
290 >     * resizing, many fewer do so). But this approximation has high
291 >     * variance for small table sizes, so we check on any collision
292 >     * for sizes <= 64. The bulk putAll operation further reduces
293 >     * contention by only committing count updates upon these size
294 >     * checks.
295       *
296       * Maintaining API and serialization compatibility with previous
297       * versions of this class introduces several oddities. Mainly: We
# Line 333 | Line 352 | public class ConcurrentHashMapV8<K, V>
352       * Encodings for special uses of Node hash fields. See above for
353       * explanation.
354       */
355 <    static final int MOVED     = 0x80000000; // hash field for fowarding nodes
355 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
356      static final int LOCKED    = 0x40000000; // set/tested only as a bit
357      static final int WAITING   = 0xc0000000; // both bits set/tested together
358      static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
# Line 373 | Line 392 | public class ConcurrentHashMapV8<K, V>
392      /**
393       * Key-value entry. Note that this is never exported out as a
394       * user-visible Map.Entry (see WriteThroughEntry and SnapshotEntry
395 <     * below). Nodes with a negative hash field are special, and do
395 >     * below). Nodes with a hash field of MOVED are special, and do
396       * not contain user keys or values.  Otherwise, keys are never
397       * null, and null val fields indicate that a node is in the
398       * process of being deleted or created. For purposes of read-only
# Line 417 | Line 436 | public class ConcurrentHashMapV8<K, V>
436           */
437          final void tryAwaitLock(Node[] tab, int i) {
438              if (tab != null && i >= 0 && i < tab.length) { // bounds check
439 +                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
440                  int spins = MAX_SPINS, h;
441                  while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
442                      if (spins >= 0) {
443 <                        if (--spins == MAX_SPINS >>> 1)
444 <                            Thread.yield();  // heuristically yield mid-way
443 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
444 >                        if (r >= 0 && --spins == 0)
445 >                            Thread.yield();  // yield before block
446                      }
447                      else if (casHash(h, h | WAITING)) {
448 <                        synchronized(this) {
448 >                        synchronized (this) {
449                              if (tabAt(tab, i) == this &&
450                                  (hash & WAITING) == WAITING) {
451                                  try {
# Line 496 | Line 517 | public class ConcurrentHashMapV8<K, V>
517       */
518      private static final int spread(int h) {
519          // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
520 +        // Despite two multiplies, this is often faster than others
521 +        // with comparable bit-spread properties.
522          h ^= h >>> 16;
523          h *= 0x85ebca6b;
524          h ^= h >>> 13;
# Line 522 | Line 545 | public class ConcurrentHashMapV8<K, V>
545          return null;
546      }
547  
525    /** Implementation for put and putIfAbsent */
526    private final Object internalPut(Object k, Object v, boolean replace) {
527        int h = spread(k.hashCode());
528        Object oldVal = null;               // previous value or null if none
529        for (Node[] tab = table;;) {
530            int i; Node f; int fh; Object fk, fv;
531            if (tab == null)
532                tab = initTable();
533            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
534                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
535                    break;                   // no lock when adding to empty bin
536            }
537            else if ((fh = f.hash) == MOVED)
538                tab = (Node[])f.key;
539            else if (!replace && (fh & HASH_BITS) == h && (fv = f.val) != null &&
540                     ((fk = f.key) == k || k.equals(fk))) {
541                oldVal = fv;                // precheck 1st node for putIfAbsent
542                break;
543            }
544            else if ((fh & LOCKED) != 0)
545                f.tryAwaitLock(tab, i);
546            else if (f.casHash(fh, fh | LOCKED)) {
547                boolean validated = false;
548                boolean checkSize = false;
549                try {
550                    if (tabAt(tab, i) == f) {
551                        validated = true;    // retry if 1st already deleted
552                        for (Node e = f;;) {
553                            Object ek, ev;
554                            if ((e.hash & HASH_BITS) == h &&
555                                (ev = e.val) != null &&
556                                ((ek = e.key) == k || k.equals(ek))) {
557                                oldVal = ev;
558                                if (replace)
559                                    e.val = v;
560                                break;
561                            }
562                            Node last = e;
563                            if ((e = e.next) == null) {
564                                last.next = new Node(h, k, v, null);
565                                if (last != f || tab.length <= 64)
566                                    checkSize = true;
567                                break;
568                            }
569                        }
570                    }
571                } finally {                  // unlock and signal if needed
572                    if (!f.casHash(fh | LOCKED, fh)) {
573                        f.hash = fh;
574                        synchronized(f) { f.notifyAll(); };
575                    }
576                }
577                if (validated) {
578                    int sc;
579                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
580                        (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc)
581                        growTable();
582                    break;
583                }
584            }
585        }
586        if (oldVal == null)
587            counter.increment();             // update counter outside of locks
588        return oldVal;
589    }
590
548      /**
549       * Implementation for the four public remove/replace methods:
550       * Replaces node value with v, conditional upon match of cv if
# Line 605 | Line 562 | public class ConcurrentHashMapV8<K, V>
562                  tab = (Node[])f.key;
563              else if ((fh & HASH_BITS) != h && f.next == null) // precheck
564                  break;                          // rules out possible existence
565 <            else if ((fh & LOCKED) != 0)
565 >            else if ((fh & LOCKED) != 0) {
566 >                checkForResize();               // try resizing if can't get lock
567                  f.tryAwaitLock(tab, i);
568 +            }
569              else if (f.casHash(fh, fh | LOCKED)) {
570                  boolean validated = false;
571                  boolean deleted = false;
# Line 639 | Line 598 | public class ConcurrentHashMapV8<K, V>
598                  } finally {
599                      if (!f.casHash(fh | LOCKED, fh)) {
600                          f.hash = fh;
601 <                        synchronized(f) { f.notifyAll(); };
601 >                        synchronized (f) { f.notifyAll(); };
602                      }
603                  }
604                  if (validated) {
605                      if (deleted)
606 <                        counter.decrement();
606 >                        counter.add(-1L);
607                      break;
608                  }
609              }
# Line 652 | Line 611 | public class ConcurrentHashMapV8<K, V>
611          return oldVal;
612      }
613  
614 <    /** Implementation for computeIfAbsent and compute. Like put, but messier. */
615 <    // Todo: Somehow reinstate non-termination check
614 >    /*
615 >     * Internal versions of the five insertion methods, each a
616 >     * little more complicated than the last. All have
617 >     * the same basic structure as the first (internalPut):
618 >     *  1. If table uninitialized, create
619 >     *  2. If bin empty, try to CAS new node
620 >     *  3. If bin stale, use new table
621 >     *  4. Lock and validate; if valid, scan and add or update
622 >     *
623 >     * The others interweave other checks and/or alternative actions:
624 >     *  * Plain put checks for and performs resize after insertion.
625 >     *  * putIfAbsent prescans for mapping without lock (and fails to add
626 >     *    if present), which also makes pre-emptive resize checks worthwhile.
627 >     *  * computeIfAbsent extends form used in putIfAbsent with additional
628 >     *    mechanics to deal with, calls, potential exceptions and null
629 >     *    returns from function call.
630 >     *  * compute uses the same function-call mechanics, but without
631 >     *    the prescans
632 >     *  * putAll attempts to pre-allocate enough table space
633 >     *    and more lazily performs count updates and checks.
634 >     *
635 >     * Someday when details settle down a bit more, it might be worth
636 >     * some factoring to reduce sprawl.
637 >     */
638 >
639 >    /** Implementation for put */
640 >    private final Object internalPut(Object k, Object v) {
641 >        int h = spread(k.hashCode());
642 >        boolean checkSize = false;
643 >        for (Node[] tab = table;;) {
644 >            int i; Node f; int fh;
645 >            if (tab == null)
646 >                tab = initTable();
647 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
648 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
649 >                    break;                   // no lock when adding to empty bin
650 >            }
651 >            else if ((fh = f.hash) == MOVED)
652 >                tab = (Node[])f.key;
653 >            else if ((fh & LOCKED) != 0) {
654 >                checkForResize();
655 >                f.tryAwaitLock(tab, i);
656 >            }
657 >            else if (f.casHash(fh, fh | LOCKED)) {
658 >                Object oldVal = null;
659 >                boolean validated = false;
660 >                try {                        // needed in case equals() throws
661 >                    if (tabAt(tab, i) == f) {
662 >                        validated = true;    // retry if 1st already deleted
663 >                        for (Node e = f;;) {
664 >                            Object ek, ev;
665 >                            if ((e.hash & HASH_BITS) == h &&
666 >                                (ev = e.val) != null &&
667 >                                ((ek = e.key) == k || k.equals(ek))) {
668 >                                oldVal = ev;
669 >                                e.val = v;
670 >                                break;
671 >                            }
672 >                            Node last = e;
673 >                            if ((e = e.next) == null) {
674 >                                last.next = new Node(h, k, v, null);
675 >                                if (last != f || tab.length <= 64)
676 >                                    checkSize = true;
677 >                                break;
678 >                            }
679 >                        }
680 >                    }
681 >                } finally {                  // unlock and signal if needed
682 >                    if (!f.casHash(fh | LOCKED, fh)) {
683 >                        f.hash = fh;
684 >                        synchronized (f) { f.notifyAll(); };
685 >                    }
686 >                }
687 >                if (validated) {
688 >                    if (oldVal != null)
689 >                        return oldVal;
690 >                    break;
691 >                }
692 >            }
693 >        }
694 >        counter.add(1L);
695 >        if (checkSize)
696 >            checkForResize();
697 >        return null;
698 >    }
699 >
700 >    /** Implementation for putIfAbsent */
701 >    private final Object internalPutIfAbsent(Object k, Object v) {
702 >        int h = spread(k.hashCode());
703 >        for (Node[] tab = table;;) {
704 >            int i; Node f; int fh; Object fk, fv;
705 >            if (tab == null)
706 >                tab = initTable();
707 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
708 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
709 >                    break;
710 >            }
711 >            else if ((fh = f.hash) == MOVED)
712 >                tab = (Node[])f.key;
713 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
714 >                     ((fk = f.key) == k || k.equals(fk)))
715 >                return fv;
716 >            else {
717 >                Node g = f.next;
718 >                if (g != null) { // at least 2 nodes -- search and maybe resize
719 >                    for (Node e = g;;) {
720 >                        Object ek, ev;
721 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
722 >                            ((ek = e.key) == k || k.equals(ek)))
723 >                            return ev;
724 >                        if ((e = e.next) == null) {
725 >                            checkForResize();
726 >                            break;
727 >                        }
728 >                    }
729 >                }
730 >                if (((fh = f.hash) & LOCKED) != 0) {
731 >                    checkForResize();
732 >                    f.tryAwaitLock(tab, i);
733 >                }
734 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
735 >                    Object oldVal = null;
736 >                    boolean validated = false;
737 >                    try {
738 >                        if (tabAt(tab, i) == f) {
739 >                            validated = true;
740 >                            for (Node e = f;;) {
741 >                                Object ek, ev;
742 >                                if ((e.hash & HASH_BITS) == h &&
743 >                                    (ev = e.val) != null &&
744 >                                    ((ek = e.key) == k || k.equals(ek))) {
745 >                                    oldVal = ev;
746 >                                    break;
747 >                                }
748 >                                Node last = e;
749 >                                if ((e = e.next) == null) {
750 >                                    last.next = new Node(h, k, v, null);
751 >                                    break;
752 >                                }
753 >                            }
754 >                        }
755 >                    } finally {
756 >                        if (!f.casHash(fh | LOCKED, fh)) {
757 >                            f.hash = fh;
758 >                            synchronized (f) { f.notifyAll(); };
759 >                        }
760 >                    }
761 >                    if (validated) {
762 >                        if (oldVal != null)
763 >                            return oldVal;
764 >                        break;
765 >                    }
766 >                }
767 >            }
768 >        }
769 >        counter.add(1L);
770 >        return null;
771 >    }
772 >
773 >    /** Implementation for computeIfAbsent */
774 >    private final Object internalComputeIfAbsent(K k,
775 >                                                 MappingFunction<? super K, ?> mf) {
776 >        int h = spread(k.hashCode());
777 >        Object val = null;
778 >        for (Node[] tab = table;;) {
779 >            Node f; int i, fh; Object fk, fv;
780 >            if (tab == null)
781 >                tab = initTable();
782 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
783 >                Node node = new Node(fh = h | LOCKED, k, null, null);
784 >                boolean validated = false;
785 >                if (casTabAt(tab, i, null, node)) {
786 >                    validated = true;
787 >                    try {
788 >                        if ((val = mf.map(k)) != null)
789 >                            node.val = val;
790 >                    } finally {
791 >                        if (val == null)
792 >                            setTabAt(tab, i, null);
793 >                        if (!node.casHash(fh, h)) {
794 >                            node.hash = h;
795 >                            synchronized (node) { node.notifyAll(); };
796 >                        }
797 >                    }
798 >                }
799 >                if (validated)
800 >                    break;
801 >            }
802 >            else if ((fh = f.hash) == MOVED)
803 >                tab = (Node[])f.key;
804 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
805 >                     ((fk = f.key) == k || k.equals(fk)))
806 >                return fv;
807 >            else {
808 >                Node g = f.next;
809 >                if (g != null) {
810 >                    for (Node e = g;;) {
811 >                        Object ek, ev;
812 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
813 >                            ((ek = e.key) == k || k.equals(ek)))
814 >                            return ev;
815 >                        if ((e = e.next) == null) {
816 >                            checkForResize();
817 >                            break;
818 >                        }
819 >                    }
820 >                }
821 >                if (((fh = f.hash) & LOCKED) != 0) {
822 >                    checkForResize();
823 >                    f.tryAwaitLock(tab, i);
824 >                }
825 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
826 >                    boolean validated = false;
827 >                    try {
828 >                        if (tabAt(tab, i) == f) {
829 >                            validated = true;
830 >                            for (Node e = f;;) {
831 >                                Object ek, ev;
832 >                                if ((e.hash & HASH_BITS) == h &&
833 >                                    (ev = e.val) != null &&
834 >                                    ((ek = e.key) == k || k.equals(ek))) {
835 >                                    val = ev;
836 >                                    break;
837 >                                }
838 >                                Node last = e;
839 >                                if ((e = e.next) == null) {
840 >                                    if ((val = mf.map(k)) != null)
841 >                                        last.next = new Node(h, k, val, null);
842 >                                    break;
843 >                                }
844 >                            }
845 >                        }
846 >                    } finally {
847 >                        if (!f.casHash(fh | LOCKED, fh)) {
848 >                            f.hash = fh;
849 >                            synchronized (f) { f.notifyAll(); };
850 >                        }
851 >                    }
852 >                    if (validated)
853 >                        break;
854 >                }
855 >            }
856 >        }
857 >        if (val == null)
858 >            throw new NullPointerException();
859 >        counter.add(1L);
860 >        return val;
861 >    }
862 >
863 >    /** Implementation for compute */
864      @SuppressWarnings("unchecked")
865 <    private final V internalCompute(K k,
866 <                                    MappingFunction<? super K, ? extends V> fn,
660 <                                    boolean replace) {
865 >    private final Object internalCompute(K k,
866 >                                         RemappingFunction<? super K, V> mf) {
867          int h = spread(k.hashCode());
868 <        V val = null;
868 >        Object val = null;
869          boolean added = false;
870 <        Node[] tab = table;
871 <        outer:for (;;) {
872 <            Node f; int i, fh; Object fk, fv;
870 >        boolean checkSize = false;
871 >        for (Node[] tab = table;;) {
872 >            Node f; int i, fh;
873              if (tab == null)
874                  tab = initTable();
875              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
# Line 672 | Line 878 | public class ConcurrentHashMapV8<K, V>
878                  if (casTabAt(tab, i, null, node)) {
879                      validated = true;
880                      try {
881 <                        val = fn.map(k);
676 <                        if (val != null) {
881 >                        if ((val = mf.remap(k, null)) != null) {
882                              node.val = val;
883                              added = true;
884                          }
# Line 682 | Line 887 | public class ConcurrentHashMapV8<K, V>
887                              setTabAt(tab, i, null);
888                          if (!node.casHash(fh, h)) {
889                              node.hash = h;
890 <                            synchronized(node) { node.notifyAll(); };
890 >                            synchronized (node) { node.notifyAll(); };
891                          }
892                      }
893                  }
# Line 691 | Line 896 | public class ConcurrentHashMapV8<K, V>
896              }
897              else if ((fh = f.hash) == MOVED)
898                  tab = (Node[])f.key;
899 <            else if (!replace && (fh & HASH_BITS) == h && (fv = f.val) != null &&
900 <                     ((fk = f.key) == k || k.equals(fk))) {
696 <                if (tabAt(tab, i) == f) {
697 <                    val = (V)fv;
698 <                    break;
699 <                }
700 <            }
701 <            else if ((fh & LOCKED) != 0)
899 >            else if ((fh & LOCKED) != 0) {
900 >                checkForResize();
901                  f.tryAwaitLock(tab, i);
902 +            }
903              else if (f.casHash(fh, fh | LOCKED)) {
904                  boolean validated = false;
705                boolean checkSize = false;
905                  try {
906                      if (tabAt(tab, i) == f) {
907                          validated = true;
908                          for (Node e = f;;) {
909 <                            Object ek, ev, v;
909 >                            Object ek, ev;
910                              if ((e.hash & HASH_BITS) == h &&
911                                  (ev = e.val) != null &&
912                                  ((ek = e.key) == k || k.equals(ek))) {
913 <                                if (replace && (v = fn.map(k)) != null)
914 <                                    ev = e.val = v;
915 <                                val = (V)ev;
913 >                                val = mf.remap(k, (V)ev);
914 >                                if (val != null)
915 >                                    e.val = val;
916                                  break;
917                              }
918                              Node last = e;
919                              if ((e = e.next) == null) {
920 <                                if ((val = fn.map(k)) != null) {
920 >                                if ((val = mf.remap(k, null)) != null) {
921                                      last.next = new Node(h, k, val, null);
922                                      added = true;
923                                      if (last != f || tab.length <= 64)
# Line 731 | Line 930 | public class ConcurrentHashMapV8<K, V>
930                  } finally {
931                      if (!f.casHash(fh | LOCKED, fh)) {
932                          f.hash = fh;
933 <                        synchronized(f) { f.notifyAll(); };
933 >                        synchronized (f) { f.notifyAll(); };
934                      }
935                  }
936 <                if (validated) {
738 <                    int sc;
739 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
740 <                        (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc)
741 <                        growTable();
936 >                if (validated)
937                      break;
743                }
938              }
939          }
940 <        if (added)
941 <            counter.increment();
940 >        if (val == null)
941 >            throw new NullPointerException();
942 >        if (added) {
943 >            counter.add(1L);
944 >            if (checkSize)
945 >                checkForResize();
946 >        }
947          return val;
948      }
949  
950 <    /**
951 <     * Implementation for clear. Steps through each bin, removing all nodes.
952 <     */
953 <    private final void internalClear() {
954 <        long delta = 0L; // negative number of deletions
955 <        int i = 0;
956 <        Node[] tab = table;
957 <        while (tab != null && i < tab.length) {
958 <            int fh;
959 <            Node f = tabAt(tab, i);
960 <            if (f == null)
961 <                ++i;
962 <            else if ((fh = f.hash) == MOVED)
963 <                tab = (Node[])f.key;
964 <            else if ((fh & LOCKED) != 0)
965 <                f.tryAwaitLock(tab, i);
966 <            else if (f.casHash(fh, fh | LOCKED)) {
967 <                boolean validated = false;
968 <                try {
969 <                    if (tabAt(tab, i) == f) {
970 <                        validated = true;
971 <                        for (Node e = f; e != null; e = e.next) {
773 <                            if (e.val != null) { // currently always true
774 <                                e.val = null;
775 <                                --delta;
776 <                            }
950 >    /** Implementation for putAll */
951 >    private final void internalPutAll(Map<?, ?> m) {
952 >        tryPresize(m.size());
953 >        long delta = 0L;     // number of uncommitted additions
954 >        boolean npe = false; // to throw exception on exit for nulls
955 >        try {                // to clean up counts on other exceptions
956 >            for (Map.Entry<?, ?> entry : m.entrySet()) {
957 >                Object k, v;
958 >                if (entry == null || (k = entry.getKey()) == null ||
959 >                    (v = entry.getValue()) == null) {
960 >                    npe = true;
961 >                    break;
962 >                }
963 >                int h = spread(k.hashCode());
964 >                for (Node[] tab = table;;) {
965 >                    int i; Node f; int fh;
966 >                    if (tab == null)
967 >                        tab = initTable();
968 >                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
969 >                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
970 >                            ++delta;
971 >                            break;
972                          }
778                        setTabAt(tab, i, null);
973                      }
974 <                } finally {
975 <                    if (!f.casHash(fh | LOCKED, fh)) {
976 <                        f.hash = fh;
977 <                        synchronized(f) { f.notifyAll(); };
974 >                    else if ((fh = f.hash) == MOVED)
975 >                        tab = (Node[])f.key;
976 >                    else if ((fh & LOCKED) != 0) {
977 >                        counter.add(delta);
978 >                        delta = 0L;
979 >                        checkForResize();
980 >                        f.tryAwaitLock(tab, i);
981 >                    }
982 >                    else if (f.casHash(fh, fh | LOCKED)) {
983 >                        boolean validated = false;
984 >                        boolean tooLong = false;
985 >                        try {
986 >                            if (tabAt(tab, i) == f) {
987 >                                validated = true;
988 >                                for (Node e = f;;) {
989 >                                    Object ek, ev;
990 >                                    if ((e.hash & HASH_BITS) == h &&
991 >                                        (ev = e.val) != null &&
992 >                                        ((ek = e.key) == k || k.equals(ek))) {
993 >                                        e.val = v;
994 >                                        break;
995 >                                    }
996 >                                    Node last = e;
997 >                                    if ((e = e.next) == null) {
998 >                                        ++delta;
999 >                                        last.next = new Node(h, k, v, null);
1000 >                                        break;
1001 >                                    }
1002 >                                    tooLong = true;
1003 >                                }
1004 >                            }
1005 >                        } finally {
1006 >                            if (!f.casHash(fh | LOCKED, fh)) {
1007 >                                f.hash = fh;
1008 >                                synchronized (f) { f.notifyAll(); };
1009 >                            }
1010 >                        }
1011 >                        if (validated) {
1012 >                            if (tooLong) {
1013 >                                counter.add(delta);
1014 >                                delta = 0L;
1015 >                                checkForResize();
1016 >                            }
1017 >                            break;
1018 >                        }
1019                      }
1020                  }
786                if (validated)
787                    ++i;
1021              }
1022 +        } finally {
1023 +            if (delta != 0)
1024 +                counter.add(delta);
1025          }
1026 <        counter.add(delta);
1026 >        if (npe)
1027 >            throw new NullPointerException();
1028      }
1029  
1030 <    /* ----------------Table Initialization and Resizing -------------- */
1030 >    /* ---------------- Table Initialization and Resizing -------------- */
1031  
1032      /**
1033       * Returns a power of two table size for the given desired capacity.
# Line 819 | Line 1056 | public class ConcurrentHashMapV8<K, V>
1056                      if ((tab = table) == null) {
1057                          int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1058                          tab = table = new Node[n];
1059 <                        sc = n - (n >>> 2) - 1;
1059 >                        sc = n - (n >>> 2);
1060                      }
1061                  } finally {
1062                      sizeCtl = sc;
# Line 831 | Line 1068 | public class ConcurrentHashMapV8<K, V>
1068      }
1069  
1070      /**
1071 <     * If not already resizing, creates next table and transfers bins.
1072 <     * Rechecks occupancy after a transfer to see if another resize is
1073 <     * already needed because resizings are lagging additions.
1074 <     */
1075 <    private final void growTable() {
1076 <        int sc = sizeCtl;
1077 <        if (sc >= 0 && UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1071 >     * If table is too small and not already resizing, creates next
1072 >     * table and transfers bins.  Rechecks occupancy after a transfer
1073 >     * to see if another resize is already needed because resizings
1074 >     * are lagging additions.
1075 >     */
1076 >    private final void checkForResize() {
1077 >        Node[] tab; int n, sc;
1078 >        while ((tab = table) != null &&
1079 >               (n = tab.length) < MAXIMUM_CAPACITY &&
1080 >               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1081 >               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1082              try {
1083 <                Node[] tab; int n;
843 <                while ((tab = table) != null &&
844 <                       (n = tab.length) > 0 && n < MAXIMUM_CAPACITY &&
845 <                       counter.sum() >= (long)sc) {
1083 >                if (tab == table) {
1084                      table = rebuild(tab);
1085 <                    sc = (n << 1) - (n >>> 1) - 1;
1085 >                    sc = (n << 1) - (n >>> 1);
1086                  }
1087              } finally {
1088                  sizeCtl = sc;
# Line 852 | Line 1090 | public class ConcurrentHashMapV8<K, V>
1090          }
1091      }
1092  
1093 +    /**
1094 +     * Tries to presize table to accommodate the given number of elements.
1095 +     *
1096 +     * @param size number of elements (doesn't need to be perfectly accurate)
1097 +     */
1098 +    private final void tryPresize(int size) {
1099 +        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1100 +            tableSizeFor(size + (size >>> 1) + 1);
1101 +        int sc;
1102 +        while ((sc = sizeCtl) >= 0) {
1103 +            Node[] tab = table; int n;
1104 +            if (tab == null || (n = tab.length) == 0) {
1105 +                n = (sc > c) ? sc : c;
1106 +                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1107 +                    try {
1108 +                        if (table == tab) {
1109 +                            table = new Node[n];
1110 +                            sc = n - (n >>> 2);
1111 +                        }
1112 +                    } finally {
1113 +                        sizeCtl = sc;
1114 +                    }
1115 +                }
1116 +            }
1117 +            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1118 +                break;
1119 +            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1120 +                try {
1121 +                    if (table == tab) {
1122 +                        table = rebuild(tab);
1123 +                        sc = (n << 1) - (n >>> 1);
1124 +                    }
1125 +                } finally {
1126 +                    sizeCtl = sc;
1127 +                }
1128 +            }
1129 +        }
1130 +    }
1131 +
1132      /*
1133       * Moves and/or copies the nodes in each bin to new table. See
1134       * above for explanation.
# Line 876 | Line 1153 | public class ConcurrentHashMapV8<K, V>
1153                          continue;
1154                  }
1155                  else {             // transiently use a locked forwarding node
1156 <                    Node g =  new Node(MOVED|LOCKED, nextTab, null, null);
1156 >                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
1157                      if (!casTabAt(tab, i, f, g))
1158                          continue;
1159                      setTabAt(nextTab, i, null);
# Line 884 | Line 1161 | public class ConcurrentHashMapV8<K, V>
1161                      setTabAt(tab, i, fwd);
1162                      if (!g.casHash(MOVED|LOCKED, MOVED)) {
1163                          g.hash = MOVED;
1164 <                        synchronized(g) { g.notifyAll(); }
1164 >                        synchronized (g) { g.notifyAll(); }
1165                      }
1166                  }
1167              }
# Line 922 | Line 1199 | public class ConcurrentHashMapV8<K, V>
1199                  } finally {
1200                      if (!f.casHash(fh | LOCKED, fh)) {
1201                          f.hash = fh;
1202 <                        synchronized(f) { f.notifyAll(); };
1202 >                        synchronized (f) { f.notifyAll(); };
1203                      }
1204                  }
1205                  if (!validated)
# Line 961 | Line 1238 | public class ConcurrentHashMapV8<K, V>
1238          }
1239      }
1240  
1241 +    /**
1242 +     * Implementation for clear. Steps through each bin, removing all
1243 +     * nodes.
1244 +     */
1245 +    private final void internalClear() {
1246 +        long delta = 0L; // negative number of deletions
1247 +        int i = 0;
1248 +        Node[] tab = table;
1249 +        while (tab != null && i < tab.length) {
1250 +            int fh;
1251 +            Node f = tabAt(tab, i);
1252 +            if (f == null)
1253 +                ++i;
1254 +            else if ((fh = f.hash) == MOVED)
1255 +                tab = (Node[])f.key;
1256 +            else if ((fh & LOCKED) != 0) {
1257 +                counter.add(delta); // opportunistically update count
1258 +                delta = 0L;
1259 +                f.tryAwaitLock(tab, i);
1260 +            }
1261 +            else if (f.casHash(fh, fh | LOCKED)) {
1262 +                boolean validated = false;
1263 +                try {
1264 +                    if (tabAt(tab, i) == f) {
1265 +                        validated = true;
1266 +                        for (Node e = f; e != null; e = e.next) {
1267 +                            if (e.val != null) { // currently always true
1268 +                                e.val = null;
1269 +                                --delta;
1270 +                            }
1271 +                        }
1272 +                        setTabAt(tab, i, null);
1273 +                    }
1274 +                } finally {
1275 +                    if (!f.casHash(fh | LOCKED, fh)) {
1276 +                        f.hash = fh;
1277 +                        synchronized (f) { f.notifyAll(); };
1278 +                    }
1279 +                }
1280 +                if (validated)
1281 +                    ++i;
1282 +            }
1283 +        }
1284 +        if (delta != 0)
1285 +            counter.add(delta);
1286 +    }
1287 +
1288 +
1289      /* ----------------Table Traversal -------------- */
1290  
1291      /**
# Line 969 | Line 1294 | public class ConcurrentHashMapV8<K, V>
1294       *
1295       * At each step, the iterator snapshots the key ("nextKey") and
1296       * value ("nextVal") of a valid node (i.e., one that, at point of
1297 <     * snapshot, has a nonnull user value). Because val fields can
1297 >     * snapshot, has a non-null user value). Because val fields can
1298       * change (including to null, indicating deletion), field nextVal
1299       * might not be accurate at point of use, but still maintains the
1300       * weak consistency property of holding a value that was once
# Line 982 | Line 1307 | public class ConcurrentHashMapV8<K, V>
1307       * value, or key-value pairs as return values of Iterator.next(),
1308       * and encapsulate the it.next check as hasNext();
1309       *
1310 <     * The iterator visits each valid node that was reachable upon
1311 <     * iterator construction once. It might miss some that were added
1312 <     * to a bin after the bin was visited, which is OK wrt consistency
1313 <     * guarantees. Maintaining this property in the face of possible
1314 <     * ongoing resizes requires a fair amount of bookkeeping state
1315 <     * that is difficult to optimize away amidst volatile accesses.
1316 <     * Even so, traversal maintains reasonable throughput.
1310 >     * The iterator visits once each still-valid node that was
1311 >     * reachable upon iterator construction. It might miss some that
1312 >     * were added to a bin after the bin was visited, which is OK wrt
1313 >     * consistency guarantees. Maintaining this property in the face
1314 >     * of possible ongoing resizes requires a fair amount of
1315 >     * bookkeeping state that is difficult to optimize away amidst
1316 >     * volatile accesses.  Even so, traversal maintains reasonable
1317 >     * throughput.
1318       *
1319       * Normally, iteration proceeds bin-by-bin traversing lists.
1320       * However, if the table has been resized, then all future steps
# Line 1026 | Line 1352 | public class ConcurrentHashMapV8<K, V>
1352              this.tab = tab;
1353              baseSize = (tab == null) ? 0 : tab.length;
1354              baseLimit = (hi <= baseSize) ? hi : baseSize;
1355 <            index = baseIndex = lo;
1355 >            index = baseIndex = (lo >= 0) ? lo : 0;
1356              next = null;
1357              advance();
1358          }
# Line 1090 | Line 1416 | public class ConcurrentHashMapV8<K, V>
1416      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
1417          this.counter = new LongAdder();
1418          this.sizeCtl = DEFAULT_CAPACITY;
1419 <        putAll(m);
1419 >        internalPutAll(m);
1420      }
1421  
1422      /**
# Line 1137 | Line 1463 | public class ConcurrentHashMapV8<K, V>
1463          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
1464              initialCapacity = concurrencyLevel;   // as estimated threads
1465          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
1466 <        int cap =  ((size >= (long)MAXIMUM_CAPACITY) ?
1467 <                    MAXIMUM_CAPACITY: tableSizeFor((int)size));
1466 >        int cap = ((size >= (long)MAXIMUM_CAPACITY) ?
1467 >                   MAXIMUM_CAPACITY: tableSizeFor((int)size));
1468          this.counter = new LongAdder();
1469          this.sizeCtl = cap;
1470      }
# Line 1257 | Line 1583 | public class ConcurrentHashMapV8<K, V>
1583      public V put(K key, V value) {
1584          if (key == null || value == null)
1585              throw new NullPointerException();
1586 <        return (V)internalPut(key, value, true);
1586 >        return (V)internalPut(key, value);
1587      }
1588  
1589      /**
# Line 1271 | Line 1597 | public class ConcurrentHashMapV8<K, V>
1597      public V putIfAbsent(K key, V value) {
1598          if (key == null || value == null)
1599              throw new NullPointerException();
1600 <        return (V)internalPut(key, value, false);
1600 >        return (V)internalPutIfAbsent(key, value);
1601      }
1602  
1603      /**
# Line 1282 | Line 1608 | public class ConcurrentHashMapV8<K, V>
1608       * @param m mappings to be stored in this map
1609       */
1610      public void putAll(Map<? extends K, ? extends V> m) {
1611 <        if (m == null)
1286 <            throw new NullPointerException();
1287 <        /*
1288 <         * If uninitialized, try to preallocate big enough table
1289 <         */
1290 <        if (table == null) {
1291 <            int size = m.size();
1292 <            int n = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1293 <                tableSizeFor(size + (size >>> 1) + 1);
1294 <            int sc = sizeCtl;
1295 <            if (n < sc)
1296 <                n = sc;
1297 <            if (sc >= 0 &&
1298 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1299 <                try {
1300 <                    if (table == null) {
1301 <                        table = new Node[n];
1302 <                        sc = n - (n >>> 2) - 1;
1303 <                    }
1304 <                } finally {
1305 <                    sizeCtl = sc;
1306 <                }
1307 <            }
1308 <        }
1309 <        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
1310 <            Object ek = e.getKey(), ev = e.getValue();
1311 <            if (ek == null || ev == null)
1312 <                throw new NullPointerException();
1313 <            internalPut(ek, ev, true);
1314 <        }
1611 >        internalPutAll(m);
1612      }
1613  
1614      /**
1615       * If the specified key is not already associated with a value,
1616 <     * computes its value using the given mappingFunction, and if
1617 <     * non-null, enters it into the map.  This is equivalent to
1618 <     *  <pre> {@code
1616 >     * computes its value using the given mappingFunction and
1617 >     * enters it into the map.  This is equivalent to
1618 >     * <pre> {@code
1619       * if (map.containsKey(key))
1620       *   return map.get(key);
1621       * value = mappingFunction.map(key);
1622 <     * if (value != null)
1326 <     *   map.put(key, value);
1622 >     * map.put(key, value);
1623       * return value;}</pre>
1624       *
1625 <     * except that the action is performed atomically.  Some attempted
1626 <     * update operations on this map by other threads may be blocked
1627 <     * while computation is in progress, so the computation should be
1628 <     * short and simple, and must not attempt to update any other
1629 <     * mappings of this Map. The most appropriate usage is to
1630 <     * construct a new object serving as an initial mapped value, or
1631 <     * memoized result, as in:
1625 >     * except that the action is performed atomically.  If the
1626 >     * function returns {@code null} (in which case a {@code
1627 >     * NullPointerException} is thrown), or the function itself throws
1628 >     * an (unchecked) exception, the exception is rethrown to its
1629 >     * caller, and no mapping is recorded.  Some attempted update
1630 >     * operations on this map by other threads may be blocked while
1631 >     * computation is in progress, so the computation should be short
1632 >     * and simple, and must not attempt to update any other mappings
1633 >     * of this Map. The most appropriate usage is to construct a new
1634 >     * object serving as an initial mapped value, or memoized result,
1635 >     * as in:
1636 >     *
1637       *  <pre> {@code
1638       * map.computeIfAbsent(key, new MappingFunction<K, V>() {
1639       *   public V map(K k) { return new Value(f(k)); }});}</pre>
# Line 1340 | Line 1641 | public class ConcurrentHashMapV8<K, V>
1641       * @param key key with which the specified value is to be associated
1642       * @param mappingFunction the function to compute a value
1643       * @return the current (existing or computed) value associated with
1644 <     *         the specified key, or {@code null} if the computation
1645 <     *         returned {@code null}
1646 <     * @throws NullPointerException if the specified key or mappingFunction
1346 <     *         is null
1644 >     *         the specified key.
1645 >     * @throws NullPointerException if the specified key, mappingFunction,
1646 >     *         or computed value is null
1647       * @throws IllegalStateException if the computation detectably
1648       *         attempts a recursive update to this map that would
1649       *         otherwise never complete
1650       * @throws RuntimeException or Error if the mappingFunction does so,
1651       *         in which case the mapping is left unestablished
1652       */
1653 +    @SuppressWarnings("unchecked")
1654      public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
1655          if (key == null || mappingFunction == null)
1656              throw new NullPointerException();
1657 <        return internalCompute(key, mappingFunction, false);
1657 >        return (V)internalComputeIfAbsent(key, mappingFunction);
1658      }
1659  
1660      /**
1661 <     * Computes the value associated with the given key using the given
1662 <     * mappingFunction, and if non-null, enters it into the map.  This
1663 <     * is equivalent to
1661 >     * Computes and enters a new mapping value given a key and
1662 >     * its current mapped value (or {@code null} if there is no current
1663 >     * mapping). This is equivalent to
1664       *  <pre> {@code
1665 <     * value = mappingFunction.map(key);
1666 <     * if (value != null)
1366 <     *   map.put(key, value);
1367 <     * else
1368 <     *   value = map.get(key);
1369 <     * return value;}</pre>
1665 >     *  map.put(key, remappingFunction.remap(key, map.get(key));
1666 >     * }</pre>
1667       *
1668 <     * except that the action is performed atomically.  Some attempted
1668 >     * except that the action is performed atomically.  If the
1669 >     * function returns {@code null} (in which case a {@code
1670 >     * NullPointerException} is thrown), or the function itself throws
1671 >     * an (unchecked) exception, the exception is rethrown to its
1672 >     * caller, and current mapping is left unchanged.  Some attempted
1673       * update operations on this map by other threads may be blocked
1674       * while computation is in progress, so the computation should be
1675       * short and simple, and must not attempt to update any other
1676 <     * mappings of this Map.
1676 >     * mappings of this Map. For example, to either create or
1677 >     * append new messages to a value mapping:
1678 >     *
1679 >     * <pre> {@code
1680 >     * Map<Key, String> map = ...;
1681 >     * final String msg = ...;
1682 >     * map.compute(key, new RemappingFunction<Key, String>() {
1683 >     *   public String remap(Key k, String v) {
1684 >     *    return (v == null) ? msg : v + msg;});}}</pre>
1685       *
1686       * @param key key with which the specified value is to be associated
1687 <     * @param mappingFunction the function to compute a value
1688 <     * @return the current value associated with
1689 <     *         the specified key, or {@code null} if the computation
1690 <     *         returned {@code null} and the value was not otherwise present
1691 <     * @throws NullPointerException if the specified key or mappingFunction
1383 <     *         is null
1687 >     * @param remappingFunction the function to compute a value
1688 >     * @return the new value associated with
1689 >     *         the specified key.
1690 >     * @throws NullPointerException if the specified key or remappingFunction
1691 >     *         or computed value is null
1692       * @throws IllegalStateException if the computation detectably
1693       *         attempts a recursive update to this map that would
1694       *         otherwise never complete
1695 <     * @throws RuntimeException or Error if the mappingFunction does so,
1695 >     * @throws RuntimeException or Error if the remappingFunction does so,
1696       *         in which case the mapping is unchanged
1697       */
1698 <    public V compute(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
1699 <        if (key == null || mappingFunction == null)
1698 >    @SuppressWarnings("unchecked")
1699 >    public V compute(K key, RemappingFunction<? super K, V> remappingFunction) {
1700 >        if (key == null || remappingFunction == null)
1701              throw new NullPointerException();
1702 <        return internalCompute(key, mappingFunction, true);
1702 >        return (V)internalCompute(key, remappingFunction);
1703      }
1704  
1705      /**
# Line 1881 | Line 2190 | public class ConcurrentHashMapV8<K, V>
2190              return true;
2191          }
2192  
2193 <        public final boolean removeAll(Collection c) {
2193 >        public final boolean removeAll(Collection<?> c) {
2194              boolean modified = false;
2195              for (Iterator<?> it = iter(); it.hasNext();) {
2196                  if (c.contains(it.next())) {
# Line 1931 | Line 2240 | public class ConcurrentHashMapV8<K, V>
2240      }
2241  
2242      static final class Values<K,V> extends MapView<K,V>
2243 <        implements Collection<V>  {
2243 >        implements Collection<V> {
2244          Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
2245          public final boolean contains(Object o) { return map.containsValue(o); }
2246  
# Line 1961 | Line 2270 | public class ConcurrentHashMapV8<K, V>
2270          }
2271      }
2272  
2273 <    static final class EntrySet<K,V>  extends MapView<K,V>
2273 >    static final class EntrySet<K,V> extends MapView<K,V>
2274          implements Set<Map.Entry<K,V>> {
2275          EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
2276  
# Line 2095 | Line 2404 | public class ConcurrentHashMapV8<K, V>
2404                          }
2405                          table = tab;
2406                          counter.add(size);
2407 <                        sc = n - (n >>> 2) - 1;
2407 >                        sc = n - (n >>> 2);
2408                      }
2409                  } finally {
2410                      sizeCtl = sc;
# Line 2103 | Line 2412 | public class ConcurrentHashMapV8<K, V>
2412              }
2413              if (!init) { // Can only happen if unsafely published.
2414                  while (p != null) {
2415 <                    internalPut(p.key, p.val, true);
2415 >                    internalPut(p.key, p.val);
2416                      p = p.next;
2417                  }
2418              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines