标签:变量 .com img 多线程 gem 竞争 抽象类 eset 更新
集合类和接口之间的关系图,能够比较清楚的展示各个类和接口之间的关系(其中:点框为接口(...) 短横线框为抽象类(---) 实线为类)
1 Comparator<? super K> cpr = comparator; 2 if (cpr != null) { 3 do { 4 parent = t; 5 cmp = cpr.compare(key, t.key); 6 if (cmp < 0) 7 t = t.left; 8 else if (cmp > 0) 9 t = t.right; 10 else 11 return t.setValue(value); 12 } while (t != null); 13 } 14 else { 15 if (key == null) 16 throw new NullPointerException(); 17 @SuppressWarnings("unchecked") 18 Comparable<? super K> k = (Comparable<? super K>) key; 19 do { 20 parent = t; 21 cmp = k.compareTo(t.key); 22 if (cmp < 0) 23 t = t.left; 24 else if (cmp > 0) 25 t = t.right; 26 else 27 return t.setValue(value); 28 } while (t != null); 29 }
int result = person1.comparTo(person2)
Comparator
PersonComparator comparator= new PersonComparator(); comparator.compare(person1,person2);
1 Map getMap(String key){ 2 if(StringUtil.isEmpty(key)){ 3 return Collections.EMPTY_MAP; 4 } 5 ... ... 6 }
1 public static <T extends Comparable<? super T>> void sort(List<T> list) { 2 list.sort(null); 3 }
1 public void sort(Comparator<? super E> c) { 2 final int expectedModCount = modCount; 3 Arrays.sort((E[]) elementData, 0, size, c); 4 if (modCount != expectedModCount) { 5 throw new ConcurrentModificationException(); 6 } 7 modCount++; 8 }
1 public static <T> void sort(T[] a, int fromIndex, int toIndex, 2 Comparator<? super T> c) { 3 if (c == null) { 4 sort(a, fromIndex, toIndex); 5 } else { 6 rangeCheck(a.length, fromIndex, toIndex); 7 if (LegacyMergeSort.userRequested) 8 legacyMergeSort(a, fromIndex, toIndex, c); 9 else 10 TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0); 11 } 12 }
1 static final class LegacyMergeSort { 2 private static final boolean userRequested = 3 java.security.AccessController.doPrivileged( 4 new sun.security.action.GetBooleanAction( 5 "java.util.Arrays.useLegacyMergeSort")).booleanValue(); 6 }
1 static class Entry<K,V> implements Map.Entry<K,V> { 2 final K key; 3 V value; 4 Entry<K,V> next; 5 int hash; 6 }
if (key == null || value == null) throw new NullPointerException();
synchronized int size(); synchronized boolean isEmpty(); ... ...
static final class Segment<K,V> extends ReentrantLock implements Serializable
1 static final class HashEntry<K,V> { 2 final K key; 3 final int hash; 4 volatile V value; 5 volatile HashEntry<K,V> next; 6 }
static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; int hash; }
1 public ConcurrentHashMap(int initialCapacity, 2 float loadFactor, int concurrencyLevel) { 3 if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0) 4 throw new IllegalArgumentException(); 5 if (concurrencyLevel > MAX_SEGMENTS) 6 concurrencyLevel = MAX_SEGMENTS; 7 // Find power-of-two sizes best matching argumentsint sshift = 0; 8 int ssize = 1; 9 while (ssize < concurrencyLevel) { 10 ++sshift; 11 ssize <<= 1; 12 } 13 this.segmentShift = 32 - sshift; 14 this.segmentMask = ssize - 1; 15 if (initialCapacity > MAXIMUM_CAPACITY) 16 initialCapacity = MAXIMUM_CAPACITY; 17 int c = initialCapacity / ssize; 18 if (c * ssize < initialCapacity) 19 ++c; 20 int cap = MIN_SEGMENT_TABLE_CAPACITY; 21 while (cap < c) 22 cap <<= 1; 23 // create segments and segments[0] 24 Segment<K,V> s0 = 25 new Segment<K,V>(loadFactor, (int)(cap * loadFactor), 26 (HashEntry<K,V>[])new HashEntry[cap]); 27 Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize]; 28 UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]this.segments = ss; 29 }
public V put(K key, V value) { Segment<K,V> s; if (value == null) throw new NullPointerException(); int hash = hash(key); int j = (hash >>> segmentShift) & segmentMask; if ((s = (Segment<K,V>)UNSAFE.getObject // nonvolatile; recheck (segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment s = ensureSegment(j); return s.put(key, hash, value, false); }
final V put(K key, int hash, V value, boolean onlyIfAbsent) { HashEntry<K,V> node = tryLock() ? null : scanAndLockForPut(key, hash, value); V oldValue; try { HashEntry<K,V>[] tab = table; int index = (tab.length - 1) & hash; HashEntry<K,V> first = entryAt(tab, index); for (HashEntry<K,V> e = first;;) { if (e != null) { K k; if ((k = e.key) == key || (e.hash == hash && key.equals(k))) { oldValue = e.value; if (!onlyIfAbsent) { e.value = value; ++modCount; } break; } e = e.next; } else { if (node != null) node.setNext(first); else node = new HashEntry<K,V>(hash, key, value, first); int c = count + 1; if (c > threshold && tab.length < MAXIMUM_CAPACITY) rehash(node); elsesetEntryAt(tab, index, node); ++modCount; count = c; oldValue = null; break; } } } finally { unlock(); } return oldValue; }
public int size() { // Try a few times to get accurate count. On failure due to// continuous async changes in table, resort to locking.final Segment<K,V>[] segments = this.segments; int size; boolean overflow; // true if size overflows 32 bitslong sum; // sum of modCountslong last = 0L; // previous sumint retries = -1; // first iteration isn‘t retrytry { for (;;) { if (retries++ == RETRIES_BEFORE_LOCK) { for (int j = 0; j < segments.length; ++j) ensureSegment(j).lock(); // force creation } sum = 0L; size = 0; overflow = false; for (int j = 0; j < segments.length; ++j) { Segment<K,V> seg = segmentAt(segments, j); if (seg != null) { sum += seg.modCount; int c = seg.count; if (c < 0 || (size += c) < 0) overflow = true; } } if (sum == last) break; last = sum; } } finally { if (retries > RETRIES_BEFORE_LOCK) { for (int j = 0; j < segments.length; ++j) segmentAt(segments, j).unlock(); } } return overflow ? Integer.MAX_VALUE : size; }
标签:变量 .com img 多线程 gem 竞争 抽象类 eset 更新
原文地址:http://www.cnblogs.com/uodut/p/7067162.html