当前位置: 代码迷 >> J2SE >> 关于非阻塞算法中的链表有关问题
  详细解决方案

关于非阻塞算法中的链表有关问题

热度:13   发布时间:2016-04-24 15:00:57.0
关于非阻塞算法中的链表问题
看到一篇文章,关于非阻塞算法的链表:
其代码如下:
(详细见:http://www-128.ibm.com/developerworks/cn/java/j-jtp04186/)

public   class   LinkedQueue   <E>   {
        private   static   class   Node   <E>   {
                final   E   item;
                final   AtomicReference <Node <E> >   next;
                Node(E   item,   Node <E>   next)   {
                        this.item   =   item;
                        this.next   =   new   AtomicReference <Node <E> > (next);
                }
        }
        private   AtomicReference <Node <E> >   head
                =   new   AtomicReference <Node <E> > (new   Node <E> (null,   null));
        private   AtomicReference <Node <E> >   tail   =   head;
        public   boolean   put(E   item)   {
                Node <E>   newNode   =   new   Node <E> (item,   null);
                while   (true)   {
                        Node <E>   curTail   =   tail.get();
                        Node <E>   residue   =   curTail.next.get();
                        if   (curTail   ==   tail.get())   {
                                if   (residue   ==   null)   /*   A   */   {
                                        if   (curTail.next.compareAndSet(null,   newNode))   /*   C   */   {
                                                tail.compareAndSet(curTail,   newNode)   /*   D   */   ;
                                                return   true;
                                        }
                                }   else   {
                                        tail.compareAndSet(curTail,   residue)   /*   B   */;
                                }
                        }
  相关解决方案