锁专题(9) ConcurrentSkipListSet 源码解析
ConcurrentSkipListSet
简介
基于以下内容的可伸缩并发NavigableSet}实现:ConcurrentSkipListMap。
集合中的元素根据其可比自然顺序或在集合创建时提供的Comparator保持排序,具体取决于使用哪个构造函数。
set 汇总
我们学习了大量的 jdk 的集合类,我们把 set 类汇总一下,便于大家对比记忆。
Set | 有序性 | 线程安全 | 底层实现 | 关键接口 | 特点 |
---|---|---|---|---|---|
HashSet | 无 | 否 | HashMap | 无 | 简单 |
LinkedHashSet | 有 | 否 | LinkedHashMap | 无 | 插入顺序 |
TreeSet | 有 | 否 | NavigableMap | NavigableSet | 自然顺序 |
CopyOnWriteArraySet | 有 | 是 | CopyOnWriteArrayList | 无 | 插入顺序,读写分离 |
ConcurrentSkipListSet | 有 | 是 | ConcurrentNavigableMap | NavigableSet | 自然顺序 |
入门例子
我们首先使用 TreeSet 做一个多线程的测试。
public class ConcurrentSkipListSetDemo {
private static Set set = new TreeSet<>();
public static void main(String[] args) {
new MyThread("a").start();
new MyThread("b").start();
}
private static class MyThread extends Thread {
MyThread(String name) {
super(name);
}
@Override
public void run() {
int i = 0;
while (i++
extends AbstractSet
implements NavigableSet, Cloneable, java.io.Serializable {
}
实现了 NavigableSet 接口,并且继承自 AbstractSet 抽象集合类。
构造器
/**
* 内部变量,ConcurrentSkipListMap 实现
*
* @author 老马啸西风
*/
private final ConcurrentNavigableMap m;
public ConcurrentSkipListSet() {
m = new ConcurrentSkipListMap();
}
public ConcurrentSkipListSet(Comparator comparator) {
m = new ConcurrentSkipListMap(comparator);
}
public ConcurrentSkipListSet(Collection c) {
m = new ConcurrentSkipListMap();
addAll(c);
}
public ConcurrentSkipListSet(SortedSet s) {
m = new ConcurrentSkipListMap(s.comparator());
addAll(s);
}
ConcurrentSkipListSet(ConcurrentNavigableMap m) {
this.m = m;
}
构造器都非常的简单,当然这里还预留了一个方法,可以使用指定的 ConcurrentNavigableMap 类来实现。
addAll 方法
实际上 addAll() 的实现非常简单
public boolean addAll(Collection c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
直接遍历集合元素,单个执行 add 方法。
如果 add 成功,则设置 modified = true;
在 ConcurrentSkipListMap 的基础之上实现,实际上源码变得很清晰。
集合大小
public int size() {
return m.size();
}
是否为空
public boolean isEmpty() {
return m.isEmpty();
}
是否包含
public boolean contains(Object o) {
return m.containsKey(o);
}
添加一个元素
public boolean add(E e) {
return m.putIfAbsent(e, Boolean.TRUE) == null;
}
为了避免内容过于朴实无华,我们就勉为其难的阅读以下 map 的实现源码。
public V putIfAbsent(K key, V value) {
return putVal(key, value, true);
}
putVal 的完整实现如下:
/**
** 这个方法被定义为 final,可见作者不希望方法被重写。
**
** @author 老马啸西风
*/
final V putVal(K key, V value, boolean onlyIfAbsent) {
// 禁止元素为 null
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
for (Node[] tab = table;;) {
Node f; int n, i, fh;
// 如果 table 为空,首先进行初始化
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(hash, key, value, null)))
break; // no lock when adding to empty bin
}
// 如果 map 处于 resize,则执行下面的方法。
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
// 使用悲观锁加锁
synchronized (f) {
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Node 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 pred = e;
if ((e = e.next) == null) {
pred.next = new Node(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
Node p;
binCount = 2;
if ((p = ((TreeBin)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
// 达到了阈值,则进行转换为树处理,默认阈值为 8.
// 类似于 HashMap 中的链表超过 8 转红黑树。
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
// 添加总数
addCount(1L, binCount);
return null;
}
其中 resizeStamp 是一个非常常用的方法:
/**
* 返回用于调整大小为n的表的标记位。
* 向左移动RESIZE_STAMP_SHIFT时必须为负。
*/
static final int resizeStamp(int n) {
return Integer.numberOfLeadingZeros(n) | (1 [] helpTransfer(Node[] tab, Node f) {
Node[] nextTab; int sc;
if (tab != null && (f instanceof ForwardingNode) &&
(nextTab = ((ForwardingNode)f).nextTable) != null) {
//返回用于调整大小为n的表的标记位。
//向左移动RESIZE_STAMP_SHIFT时必须为负。
int rs = resizeStamp(tab.length);
while (nextTab == nextTable && table == tab &&
(sc = sizeCtl) >> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || transferIndex [] tab, Node[] nextTab) {
int n = tab.length, stride;
// 这里会根据 CPU 的核数,进行选择
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) [] nt = (Node[])new Node[n fwd = new ForwardingNode(nextTab);
boolean advance = true;
boolean finishing = false; // to ensure sweep before committing nextTab
for (int i = 0, bound = 0;;) {
Node f; int fh;
while (advance) {
int nextIndex, nextBound;
if (--i >= bound || finishing)
advance = false;
else if ((nextIndex = transferIndex) stride ?
nextIndex - stride : 0))) {
bound = nextBound;
i = nextIndex - 1;
advance = false;
}
}
if (i = n || i + n >= nextn) {
int sc;
// finishing 标识是否已经完成,如果完成就直接返回了。
if (finishing) {
nextTable = null;
table = nextTab;
sizeCtl = (n >> 1);
return;
}
//CAS 设置值。
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
if ((sc - 2) != resizeStamp(n) ln, hn;
if (fh >= 0) {
int runBit = fh & n;
Node lastRun = f;
// 遍历所有节点,更新对应的信息。
for (Node p = f.next; p != null; p = p.next) {
int b = p.hash & n;
if (b != runBit) {
runBit = b;
lastRun = p;
}
}
if (runBit == 0) {
ln = lastRun;
hn = null;
}
else {
hn = lastRun;
ln = null;
}
for (Node p = f; p != lastRun; p = p.next) {
int ph = p.hash; K pk = p.key; V pv = p.val;
if ((ph & n) == 0)
ln = new Node(ph, pk, pv, ln);
else
hn = new Node(ph, pk, pv, hn);
}
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
else if (f instanceof TreeBin) {
// 如果就是 TreeBin 的处理逻辑。
TreeBin t = (TreeBin)f;
TreeNode lo = null, loTail = null;
TreeNode hi = null, hiTail = null;
int lc = 0, hc = 0;
for (Node e = t.first; e != null; e = e.next) {
int h = e.hash;
TreeNode p = new TreeNode
(h, e.key, e.val, null, null);
if ((h & n) == 0) {
if ((p.prev = loTail) == null)
lo = p;
else
loTail.next = p;
loTail = p;
++lc;
}
else {
if ((p.prev = hiTail) == null)
hi = p;
else
hiTail.next = p;
hiTail = p;
++hc;
}
}
ln = (lc (lo) : t;
hn = (hc (hi) : t;
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}
treeifyBin
/**
* 除非表太小,否则以给定的索引替换bin中所有链接的节点,在这种情况下,将调整大小。
* @author 老马啸西风
*/
private final void treeifyBin(Node[] tab, int index) {
Node b; int n, sc;
if (tab != null) {
if ((n = tab.length) = 0) {
// 悲观锁同步
synchronized (b) {
// UNSAFE 获取元素
if (tabAt(tab, index) == b) {
TreeNode hd = null, tl = null;
for (Node e = b; e != null; e = e.next) {
TreeNode p =
new TreeNode(e.hash, e.key, e.val,
null, null);
if ((p.prev = tl) == null)
hd = p;
else
tl.next = p;
tl = p;
}
//CAS 设置
setTabAt(tab, index, new TreeBin(hd));
}
}
}
}
}
tryPresize 这个方法也值得展开一下,实现如下:
/**
* 尝试调整表的大小以容纳给定数量的元素。
* 为什么不需要特别准确,个人理解这里应该是翻倍扩容。所以数据有一点误差,问题不大。
*
* @param size 元素数量(不需要完全准确)
* @author 老马啸西风
*/
private final void tryPresize(int size) {
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
tableSizeFor(size + (size >>> 1) + 1);
int sc;
while ((sc = sizeCtl) >= 0) {
Node[] tab = table; int n;
if (tab == null || (n = tab.length) == 0) {
n = (sc > c) ? sc : c;
if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
try {
if (table == tab) {
@SuppressWarnings("unchecked")
Node[] nt = (Node[])new Node[n];
table = nt;
sc = n - (n >>> 2);
}
} finally {
sizeCtl = sc;
}
}
}
else if (c = MAXIMUM_CAPACITY)
break;
else if (tab == table) {
int rs = resizeStamp(n);
if (sc [] nt;
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
transferIndex = 0) {
Node[] tab, nt; int n, sc;
while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
(n = tab.length) >> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
transferIndex 0) {
if ((a = as[(n - 1) & h]) == null) {
if (cellsBusy == 0) { // Try to attach new Cell
CounterCell r = new CounterCell(x); // Optimistic create
// 通过 CAS 设置变量信息
if (cellsBusy == 0 &&
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean created = false;
try { // Recheck under lock
CounterCell[] rs; int m, j;
if ((rs = counterCells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
cellsBusy = 0;
}
// 这里实际上是通过循环+CAS一直尝试创建,成功则跳出循环。
if (created)
break;
continue; // Slot is now non-empty
}
}
collide = false;
}
else if (!wasUncontended) // CAS already known to fail
wasUncontended = true; // Continue after rehash
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
break;
else if (counterCells != as || n >= NCPU)
collide = false; // At max size or stale
else if (!collide)
collide = true;
else if (cellsBusy == 0 &&
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
try {
if (counterCells == as) {// Expand table unless stale
CounterCell[] rs = new CounterCell[n << 1];
for (int i = 0; i < n; ++i)
rs[i] = as[i];
counterCells = rs;
}
} finally {
cellsBusy = 0;
}
collide = false;
continue; // Retry with expanded table
}
h = ThreadLocalRandom.advanceProbe(h);
}
else if (cellsBusy == 0 && counterCells == as &&
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
boolean init = false;
try { // Initialize table
if (counterCells == as) {
CounterCell[] rs = new CounterCell[2];
rs[h & 1] = new CounterCell(x);
counterCells = rs;
init = true;
}
} finally {
cellsBusy = 0;
}
if (init)
break;
}
else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
break; // Fall back on using base
}
}
sumCount 计算总数
这个方法相对比较简单,遍历集合,然后累加。
final long sumCount() {
CounterCell[] as = counterCells; CounterCell a;
long sum = baseCount;
if (as != null) {
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
sum += a.value;
}
}
return sum;
}
移除一个元素
public boolean remove(Object o) {
return m.remove(o, Boolean.TRUE);
}
这里参考 ConcurrentSkipListMap 的源码,此处不做展开。
清空
public void clear() {
m.clear();
}
小结
好家伙,ConcurrentSkipListSet 源码说简单是非常简单。说复杂,也可以说是非常复杂。
我们只是针对一个 put 方法进行展开,内容就已经非常多了。
阅读源码,才感觉知识储备太少了,读起来有些地方非常吃力。自己需要补的知识还是非常多的。
知道自己无知是第一步,学习去弥补这个无知是第二步。
希望本文对你有帮助,如果有其他想法的话,也可以评论区和大家分享哦。
各位极客的点赞收藏转发,是老马持续写作的最大动力!
参考资料
jdk 源码
ConcurrentSkipListMap 源码分析 (基于Java 8)