java中的非阻塞堆栈

java中的非阻塞堆栈,java,multithreading,compare-and-swap,Java,Multithreading,Compare And Swap,有人能帮我写一个无阻塞、无锁的堆栈实现吗。它在Sun java实现中存在吗 我试图通过在整个堆栈数据结构上设置全局锁来编写线程安全的堆栈(成本很高),但似乎可以编写一个无阻塞、无锁的堆栈 如果一个算法没有锁且不受死锁影响,则称为非阻塞。公共类MyConcurrentStack{ public class MyConcurrentStack<T> { private AtomicReference<Node> head = new AtomicReference&

有人能帮我写一个无阻塞、无锁的堆栈实现吗。它在Sun java实现中存在吗

我试图通过在整个堆栈数据结构上设置全局锁来编写线程安全的
堆栈
(成本很高),但似乎可以编写一个无阻塞、无锁的堆栈

如果一个算法没有锁且不受死锁影响,则称为非阻塞。

公共类MyConcurrentStack{
public class MyConcurrentStack<T> {

    private AtomicReference<Node> head = new AtomicReference<Node>();

    public MyConcurrentStack() {
    }

    public void push(T t) {
        if (t == null) {
            return;
        }
        Node<T> n = new Node<T>(t);
        Node<T> current;

        do {
            current = head.get();
            n.setNext(current);
        } while (!head.compareAndSet(current, n));
    }

    public T pop() {
        Node<T> currentHead = null;
        Node<T> futureHead = null;
        do {
            currentHead = head.get();
            if (currentHead == null) {
                return null;
            }
            futureHead = currentHead.next;
        } while (!head.compareAndSet(currentHead, futureHead));

        return currentHead.data;
    }

    /**
     *
     * @return null if no element present else return a element. it does not
     * remove the element from the stack.
     */
    public T peek() {
        Node<T> n = head.get();
        if (n == null) {
            return null;
        } else {
            return n.data;
        }
    }

    public boolean isEmpty() {
        if (head.get() == null) {
            return true;
        }
        return false;
    }

    private static class Node<T> {

        private final T data;
        private Node<T> next;

        private Node(T data) {
            this.data = data;
        }

        private void setNext(Node next) {
            this.next = next;
        }
    }
}
私有原子引用头=新原子引用(); 公共MyConcurrentStack(){ } 公共无效推送(T){ 如果(t==null){ 返回; } 节点n=新节点(t); 节点电流; 做{ 当前=head.get(); n、 setNext(当前); }而(!head.compareAndSet(current,n)); } 公共广播电台{ 节点currentHead=null; 节点未来头=空; 做{ currentHead=head.get(); 如果(currentHead==null){ 返回null; } futureHead=currentHead.next; }而(!head.compareAndSet(currentHead,futureHead)); 返回currentHead.data; } /** * *@return null如果不存在元素,则返回元素。它不会 *从堆栈中删除元素。 */ 公共T peek(){ 节点n=head.get(); 如果(n==null){ 返回null; }否则{ 返回n.data; } } 公共布尔值为空(){ if(head.get()==null){ 返回true; } 返回false; } 私有静态类节点{ 私有最终T数据; 私有节点下一步; 专用节点(T数据){ 这个数据=数据; } 私有void setNext(节点next){ this.next=next; } } }
并发堆栈java第一个google结果

排队

concurrent类提供了一个 高效可扩展的线程安全非阻塞FIFO队列。五 java.util.concurrent中的实现支持扩展的 接口,该接口定义put的阻塞版本 就拿,, ,及 . 不同的类涵盖了最常见的用法 生产者-消费者、消息传递、并行任务和 相关并行设计。BlockingDeque接口将扩展 BlockingQueue支持FIFO和LIFO(基于堆栈的)操作。 类LinkedBlockingDeque提供了一个实现


OP询问堆栈,即后进先出。再次读取
BlockingDeque接口扩展BlockingQueue以支持FIFO和后进先出(基于堆栈)操作。类LinkedBlockingDeque提供了一个实现。
LinkedBlockingDeque
不是我想要的实现。我想要非阻塞和无锁的实现,而
LinkedBlockingDeque
不是@尝试已经给出了最接近的答案,但我仍在等待是否还有其他可能。谢谢。ConcurrentLinkedQueue实现FIFO,而LIFI是必需的,LinkedBlockingDequeue是阻塞的,而非阻塞是必需的。有了设施,责任就来了。虽然SO上的用户总是在那里提供帮助,但一点google&对结果的分析是必须的。请重新打开我重新表述的问题。谢谢。这篇优秀的文章提供了非阻塞堆栈的解决方案。这是一个相关的问题,我通过谷歌搜索专门针对这个问题,我发现它被锁定是错误的。这不是一个“学校作业”问题,关于ConcurrentLinkedQueue的答案完全偏离了一个非常具体和有趣的问题。