1.采用数组 + 链表/红黑树结构
//数组
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;
}
//红黑树
static final class TreeBin<K,V> extends Node<K,V> {
TreeNode<K,V> root;
volatile TreeNode<K,V> first;
}
static final class TreeNode<K,V> extends Node<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
}
2.采用优化后的synchronized和CAS技术实现线程安全
3.CAS=CompareAndSwap,比较并替换。根据一个内存地址,查看里面的值是否跟预期值相等,相等则替换新值,否则不做任何操作。类似数据库乐观锁。