Treiber算法实现Stack

package com.lss.test1;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 使用Treiber算法
 * Treiber算法主要用于实现Stack,基于Treiber算法实现的无阻塞的Stack
 * @author Administrator
 *
 * @param <E>
 */
public class CurrentStack<E> {
 AtomicReference<Node<E>> head = new AtomicReference<Node<E>>();

 public void push(E item) {
  Node<E> newHead = new Node<E>(item);
  Node<E> oldHead;
  do {
   oldHead = head.get();
   newHead.next = oldHead;
  } while (!head.compareAndSet(oldHead, newHead));
 }

 static class Node<E> {
  final E item;
  Node<E> next;

  public Node(E item) {
   this.item = item;
  }
 }
}

你可能感兴趣的:(Treiber算法实现Stack)