PDF
HashTable1ConcurrentHashMapContentsCollections.synchronizedMap ....................................................................... 1HashTable ............................................................................................. 1ConcurrentHashMap ................................................................................. 2线线Collections.synchronizedMapprivate static class SynchronizedMap<K,V> implements Map<K,V>, Serializable { private final Map<K,V> m; // Backing Map final Object mutex; SynchronizedMap(Map<K,V> m) { this.m = Objects.requireNonNull(m); mutex = this; }public boolean containsValue(Object value) { synchronized (mutex) {return m.containsValue(value);}}public V get(Object key) { synchronized (mutex) {return m.get(key);}}// ...HashTable线public synchronized int size() { return count;} ConcurrentHashMap2get(key)contains(key)线ConcurrentHashMap使getpublic V get(Object key) { Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; // hash int h = spread(key.hashCode()); if ((tab = table) != null && (n = tab.length) > 0 && // (n-1) & h indexHashMap (e = tabAt(tab, (n - 1) & h)) != null) { // equal if ((eh = e.hash) == h) { // keyequalhashCode // equals if ((ek = e.key) == key || (ek != null && key.equals(ek))) return e.val; } /* Hashhash ConcurrentHashMap3 */ // else if (eh < 0) return (p = e.find(h, key)) != null ? p.val : null; // while ((e = e.next) != null) { if (e.hash == h && ((ek = e.key) == key || (ek != null && key.equals(ek)))) return e.val; } } return null;}transient volatile Node<K,V>[] table;transient volatile Node<K,V>[] table;static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; volatile V val; volatile Node<K,V> next; //...}putfinal V putVal(K key, V value, boolean onlyIfAbsent) { if (key == null || value == null) throw new NullPointerException(); int hash = spread(key.hashCode()); int binCount = 0; for (Node<K,V>[] tab = table;;) { Node<K,V> f; int n, i, fh; // if (tab == null || (n = tab.length) == 0) tab = initTable(); // else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { // CAS if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value, null))) break; // no lock when adding to empty bin } // else if ((fh = f.hash) == MOVED) tab = helpTransfer(tab, f); else { V oldVal = null; ConcurrentHashMap4 // f synchronized (f) { // if (tabAt(tab, i) == f) { // if (fh >= 0) { binCount = 1; for (Node<K,V> e = f;; ++binCount) { K ek; // if (e.hash == hash && ((ek = e.key) == key || (ek != null && key.equals(ek)))) { oldVal = e.val; if (!onlyIfAbsent) e.val = value; break; } // Node<K,V> pred = e; if ((e = e.next) == null) { pred.next = new Node<K,V>(hash, key, value, null); break; } } } // else if (f instanceof TreeBin) { Node<K,V> p; binCount = 2; if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key, value)) != null) { oldVal = p.val; if (!onlyIfAbsent) p.val = value; } } } } if (binCount != 0) { if (binCount >= TREEIFY_THRESHOLD) treeifyBin(tab, i); if (oldVal != null) return oldVal; break; } } } addCount(1L, binCount); ConcurrentHashMap5 return null;}

HTML view coming soon.

Download PDF for the full formatted version.