DefaultAttributeMap :
private volatile AtomicReferenceArray> attributes;
寻址方式:
Attributekey父类:
public abstract class AbstractConstant> implements Constant {
...
@Override
public final int id() {
return id;
}
DefaultAttributeMap里的"table":
attributes = new AtomicReferenceArray>(BUCKET_SIZE);
...
private static final int BUCKET_SIZE = 4;
private static final int MASK = BUCKET_SIZE - 1;
...
index寻址函数:
与运算相比hashmap中的hashcode操作,效率更高
return key.id() & MASK;
乐观锁:
cas:
attributes.compareAndSet(i, null, head)
悲观锁:
synchronized:
synchronized (head) {
DefaultAttribute> curr = head;
for (;;) {
DefaultAttribute> next = curr.next;
if (next == null) {
DefaultAttributeattr = new DefaultAttribute (head, key);
curr.next = attr;
attr.prev = curr;
return attr;
}
if (next.key == key && !next.removed) {
return (Attribute) next;
}
curr = next;
}
}
ConcurrentHashMap :
transient volatile Node[] table;
乐观锁:
cas:
casTabAt(tab, i, null,new Node(hash, key, value, null))
悲观锁:
synchronized:
synchronized (f) {
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Nodee = 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;
}
Nodepred = e;
if ((e = e.next) == null) {
pred.next = new Node(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
Nodep;
binCount = 2;
if ((p = ((TreeBin)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
hashmap寻址方式自然是hashcode()