JDK 源码理解问题

2016/12 01 13:12
此文章不得转载,如有需要请联系网站管理员。

一度的温暖,一百度的爱情,来自于sbf999胜博发pt娱乐老虎机的点点滴滴

小弟近来在翻看JDK的源码但是如何怎样根底太差有一处一直想不通。盼望大家帮忙解答下共同进步。

ConcurrentLinkedQueue的源码中的offer办法
 (checkNotNulle;
        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.casNextnull, 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
                        casTailt, newNode;  // Failure is OK.
                    return true;
                }
                // Lost CAS race to another thread; re-read next
            }
            )
我的理解是 既然t = p; 那么对p的操纵应该同等与对t的操纵,那么将newNode设置为p.next不久同等于对t也设置next属性么, 为什么p!=t呢?

就一个线程在处理的话一定不会走casTail这句,假如有多个线程呢?

    public boolean offerE e {
        checkNotNulle;
        final Node<E> newNode = new Node<E>e;

        for Node<E> t = tail, p = t;; {
             //假如2个线程到这里,一个先把流程走完了
             //第二个线程的q末尾实行的时分曾经不是null了
             //以是会走else的分支,修正了p和t的干系
            Node<E> q = p.next;
            if q == null {
                // p is last node
                if p.casNextnull, 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
                        casTailt, 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;
        }
    }

(看完/读完)这篇文章有何感想! 来看看sbf999胜博发pt娱乐老虎机是怎么评论的吧!

--转载请注明: sbf999胜博发老虎机_胜博发娱乐老虎机_sbf999胜博发pt娱乐老虎机 » JDK 源码理解问题

发表评论

(必填)