分析Java非阻塞算法Lock-Free的實(shí)現(xiàn)
我們先使用CAS來構(gòu)建幾個(gè)非阻塞的棧。棧是最簡(jiǎn)單的鏈?zhǔn)浇Y(jié)構(gòu),其本質(zhì)是一個(gè)鏈表,而鏈表的根節(jié)點(diǎn)就是棧頂。
我們先構(gòu)建Node數(shù)據(jù)結(jié)構(gòu):
public class Node<E> { public final E item; public Node<E> next; public Node(E item){this.item=item; }}
這個(gè)Node保存了內(nèi)存item和它的下一個(gè)節(jié)點(diǎn)next。
然后我們構(gòu)建非阻塞的棧,在該棧中我們需要實(shí)現(xiàn)pop和push方法,我們使用一個(gè)Atomic類來保存top節(jié)點(diǎn)的引用,在pop和push之前調(diào)用compareAndSet命令來保證命令的原子性。同時(shí),我們需要不斷的循環(huán),以保證在線程沖突的時(shí)候能夠重試更新。
public class ConcurrentStack<E> { AtomicReference<Node<E>> top= new AtomicReference<>(); public void push(E item){Node<E> newNode= new Node<>(item);Node<E> oldNode;do{ oldNode=top.get(); newNode.next= oldNode;}while(!top.compareAndSet(oldNode, newNode)); } public E pop(){Node<E> oldNode;Node<E> newNode;do { oldNode = top.get(); if(oldNode == null){return null; } newNode=oldNode.next;}while(!top.compareAndSet(oldNode, newNode));return oldNode.item; }}非阻塞的鏈表
構(gòu)建鏈表要比構(gòu)建棧復(fù)雜。因?yàn)槲覀円S持頭尾兩個(gè)指針。以put方法來說,我們需要執(zhí)行兩步操作:1. 在尾部插入新的節(jié)點(diǎn)。2.將尾部指針指向最新的節(jié)點(diǎn)。
我們使用CAS最多只能保證其中的一步是原子執(zhí)行。那么對(duì)于1和2的組合步驟該怎么處理呢?
我們?cè)僮屑?xì)考慮考慮,其實(shí)1和2并不一定要在同一個(gè)線程中執(zhí)行,其他線程在檢測(cè)到有線程插入了節(jié)點(diǎn),但是沒有將tail指向最后的節(jié)點(diǎn)時(shí),完全幫忙完成這個(gè)操作。
我們看下具體的代碼實(shí)現(xiàn):
public class LinkedNode<E> { public final E item; public final AtomicReference<LinkedNode<E>> next; public LinkedNode(E item, LinkedNode<E> next){this.item=item;this.next=new AtomicReference<>(next); }}
先構(gòu)建一個(gè)LinkedNode類。
public class LinkedQueue<E> { private final LinkedNode<E> nullNode= new LinkedNode<>(null, null); private final AtomicReference<LinkedNode<E>> head= new AtomicReference<>(nullNode); private final AtomicReference<LinkedNode<E>> tail= new AtomicReference<>(nullNode); public boolean put(E item){ LinkedNode<E> newNode = new LinkedNode<>(item, null); while (true){LinkedNode<E> currentTail= tail.get();LinkedNode<E> tailNext= currentTail.next.get();if(currentTail == tail.get()){ if (tailNext != null) {//有其他的線程已經(jīng)插入了一個(gè)節(jié)點(diǎn),但是還沒有將tail指向最新的節(jié)點(diǎn)tail.compareAndSet(currentTail, tailNext); }else{//沒有其他的線程插入節(jié)點(diǎn),那么做兩件事情:1. 插入新節(jié)點(diǎn),2.將tail指向最新的節(jié)點(diǎn)if(currentTail.next.compareAndSet(null, newNode)){ tail.compareAndSet(currentTail, newNode);} }} } }}
以上就是分析Java非阻塞算法Lock-Free的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Java非阻塞算法Lock-Free的實(shí)現(xiàn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 如何對(duì)php程序中的常見漏洞進(jìn)行攻擊2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能4. ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)5. JavaWeb Servlet中url-pattern的使用6. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法7. jsp EL表達(dá)式詳解8. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法9. JSP之表單提交get和post的區(qū)別詳解及實(shí)例10. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算
