zoukankan      html  css  js  c++  java
  • 无锁同步栈实现

    public class ConcurrentStack<E> {
        private AtomicReference<Node<E>> top = new AtomicReference<Node<E>>();
    
        ConcurrentStack(){
        }
        
        public void push(E item) {
            Node<E> newHead = new Node<E>(item);
            Node<E> oldHead = null;
            do {
                oldHead = top.get();
                newHead.next = oldHead;
            } while (!top.compareAndSet(oldHead, newHead));
        }
    
        public E pop() {
            Node<E> newHead = null;
            Node<E> oldHead = null;
            do {
                oldHead = top.get();
                if (oldHead == null) { // return null;
                    continue;
                }
                newHead = oldHead.next;
            } while (oldHead == null || !top.compareAndSet(oldHead, newHead));
            return oldHead.item;
        }
    
        private static class Node<E> {
            public final E item;
            public Node<E> next;
    
            public Node(E item) {
                this.item = item;
            }
        }
    }


  • 相关阅读:
    博客园的博客
    JAVASCRIPT拷贝内容到剪切板
    adobe acrobat pro 8.0/8.1 激活
    lighttpd+PHP上传文件
    windows上UPNP/DLNA服务器软件测试
    maven环境搭建
    Chrome调试博客地址
    struts2技术内幕读书笔记1
    git推送
    第九周助教小结
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3013896.html
Copyright © 2011-2022 走看看