寫這篇文章源于我經(jīng)歷過(guò)的一次生產(chǎn)事故盆佣,在某家公司的時(shí)候,有個(gè)服務(wù)會(huì)收集業(yè)務(wù)系統(tǒng)的日志械荷,此服務(wù)的開(kāi)發(fā)人員在給業(yè)務(wù)系統(tǒng)的sdk中就因?yàn)槭褂昧薒inkedList共耍,又沒(méi)有做并發(fā)控制,就造成了此服務(wù)經(jīng)常不能正常收集到業(yè)務(wù)系統(tǒng)的日志(丟日志以及日志上報(bào)的線程停止運(yùn)行)吨瞎”远担看一下add()方法的源碼,我們就可以知道原因了:
public boolean add(E e) {
linkLast(e);//調(diào)用linkLast颤诀,在隊(duì)列尾部添加元素
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;//多線程情況下字旭,如果業(yè)務(wù)系統(tǒng)沒(méi)做并發(fā)控制,size的數(shù)量會(huì)遠(yuǎn)遠(yuǎn)大于實(shí)際元素的數(shù)量
modCount++;
}
demo Lesson2LinkedListThreads 展示了在多線程且沒(méi)有做并發(fā)控制的環(huán)境下崖叫,size的值遠(yuǎn)遠(yuǎn)大于了隊(duì)列的實(shí)際值遗淳,100個(gè)線程,每個(gè)添加1000個(gè)元素心傀,最后實(shí)際只加進(jìn)去2030個(gè)元素:
List的變量size值為:88371
第2031個(gè)元素取出為null
解決方案屈暗,使用鎖或者使用ConcurrentLinkedQueue、LinkedBlockingQueue等支持添加元素為原子操作的隊(duì)列脂男。
上一節(jié)我們已經(jīng)分析過(guò)LinkedBlockingQueue的put等方法的源碼养叛,是使用ReentrantLock來(lái)實(shí)現(xiàn)的添加元素原子操作。我們?cè)俸?jiǎn)單看一下高并發(fā)queue的add和offer()方法宰翅,方法中使用了CAS來(lái)實(shí)現(xiàn)的無(wú)鎖的原子操作:
public boolean add(E e) {
return offer(e);
}
public boolean offer(E e) {
checkNotNull(e);
final Node<E> newNode = new Node<E>(e);
for (Node<E> t = tail, p = t;;) {
Node<E> q = p.next;
if (q == null) {
// p is last node
if (p.casNext(null, newNode)) {
// Successful CAS is the linearization point
// for e to become an element of this queue,
// and for newNode to become "live".
if (p != t) // hop two nodes at a time
casTail(t, newNode); // Failure is OK.
return true;
}
// Lost CAS race to another thread; re-read next
}
else if (p == q)
// We have fallen off list. If tail is unchanged, it
// will also be off-list, in which case we need to
// jump to head, from which all live nodes are always
// reachable. Else the new tail is a better bet.
p = (t != (t = tail)) ? t : head;
else
// Check for tail updates after two hops.
p = (p != t && t != (t = tail)) ? t : q;
}
}
接下來(lái)弃甥,我們?cè)倮酶卟l(fā)queue對(duì)上面的demo進(jìn)行改造,大家只要改變demo中的內(nèi)容汁讼,講下面兩行的注釋內(nèi)容顛倒淆攻,即可發(fā)現(xiàn)沒(méi)有丟失任何的元素:
public static LinkedList list = new LinkedList();
//public static ConcurrentLinkedQueue list = new ConcurrentLinkedQueue();
再看一下高性能queue的poll()方法肮之,才覺(jué)得NB,取元素的方法也用CAS實(shí)現(xiàn)了原子操作卜录,因此在實(shí)際使用的過(guò)程中戈擒,當(dāng)我們?cè)诓荒敲丛谝庠靥幚眄樞虻那闆r下,隊(duì)列元素的消費(fèi)者艰毒,完全可以是多個(gè)筐高,不會(huì)丟任何數(shù)據(jù):
public E poll() {
restartFromHead:
for (;;) {
for (Node<E> h = head, p = h, q;;) {
E item = p.item;
if (item != null && p.casItem(item, null)) {
// Successful CAS is the linearization point
// for item to be removed from this queue.
if (p != h) // hop two nodes at a time
updateHead(h, ((q = p.next) != null) ? q : p);
return item;
}
else if ((q = p.next) == null) {
updateHead(h, p);
return null;
}
else if (p == q)
continue restartFromHead;
else
p = q;
}
}
}
關(guān)于ConcurrentLinkedQueue和LinkedBlockingQueue:
1.LinkedBlockingQueue是使用鎖機(jī)制,ConcurrentLinkedQueue是使用CAS算法丑瞧,雖然LinkedBlockingQueue的底層獲取鎖也是使用的CAS算法
2.關(guān)于取元素柑土,ConcurrentLinkedQueue不支持阻塞去取元素LinkedBlockingQueue支持阻塞的take()方法,如若大家需要
ConcurrentLinkedQueue的消費(fèi)者產(chǎn)生阻塞效果绊汹,需要自行實(shí)現(xiàn)
3.關(guān)于插入元素的性能稽屏,從字面上和代碼簡(jiǎn)單的分析來(lái)看ConcurrentLinkedQueue肯定是最快的,但是這個(gè)也要看具體的測(cè)試場(chǎng)景西乖,我做了兩個(gè)簡(jiǎn)單的demo做測(cè)試狐榔,測(cè)試的結(jié)果如下,兩個(gè)的性能差不多获雕,但在實(shí)際的使用過(guò)程中薄腻,尤其在多cpu的服務(wù)器上,有鎖和無(wú)鎖的差距便體現(xiàn)出來(lái)了届案,ConcurrentLinkedQueue會(huì)比LinkedBlockingQueue快很多:
demo Lesson2ConcurrentLinkedQueuePerform:在使用ConcurrentLinkedQueue的情況下100個(gè)線程循環(huán)增加的元素?cái)?shù)為:33828193
demo Lesson2LinkedBlockingQueuePerform:在使用LinkedBlockingQueue的情況下100個(gè)線程循環(huán)增加的元素?cái)?shù)為:33827382