Advertisement
Jaydeep999997

Non Blocking Stack - AtomicReference

Nov 2nd, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | Source Code | 0 0
  1. import java.util.concurrent.BrokenBarrierException;
  2. import java.util.concurrent.CyclicBarrier;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.TimeUnit;
  6. import java.util.concurrent.atomic.AtomicReference;
  7.  
  8. class Demonstration {
  9.   public static void main(String[] args) throws Exception {
  10.     Stack<Integer> stack = new Stack<Integer>();
  11.     ExecutorService executor = Executors.newFixedThreadPool(20);
  12.     int numThreads = 2;
  13.     CyclicBarrier barrier = new CyclicBarrier(numThreads);
  14.     try {
  15.       for (int i = 0; i < numThreads; i++) {
  16.         executor.submit(
  17.             new Runnable() {
  18.               @Override
  19.               public void run() {
  20.                 for (int j = 0; j < 1000; j++) {
  21.                   stack.push(j);
  22.                 }
  23.                 try {
  24.                   barrier.await();
  25.                 } catch (InterruptedException e) {
  26.  
  27.                 } catch (BrokenBarrierException e) {
  28.  
  29.                 }
  30.  
  31.                 for (int j = 0; j < 1000; j++) {
  32.                   stack.pop();
  33.                 }
  34.               }
  35.             });
  36.       }
  37.     } finally {
  38.       executor.shutdown();
  39.       executor.awaitTermination(10, TimeUnit.SECONDS);
  40.     }
  41.   }
  42. }
  43.  
  44. class Stack<T> {
  45.   private AtomicReference<StackNode<T>> topNode = new AtomicReference<StackNode<T>>(null);
  46.  
  47.   public void push(T item) {
  48.     StackNode<T> node, currentTop;
  49.  
  50.     do {
  51.       node = new StackNode<T>(item);
  52.       currentTop = topNode.get();
  53.       if (currentTop != null) {
  54.         node.setNext(currentTop);
  55.       }
  56.     } while (!topNode.compareAndSet(currentTop, node));
  57.   }
  58.  
  59.   public T pop() {
  60.     if (topNode.get() == null) {
  61.       return null;
  62.     }
  63.     StackNode<T> currentTop;
  64.     StackNode<T> newTop;
  65.     do {
  66.       currentTop = topNode.get();
  67.       if (currentTop == null) {
  68.         return null;
  69.       }
  70.       newTop = currentTop.getNext();
  71.     } while (!topNode.compareAndSet(currentTop, newTop));
  72.  
  73.     return currentTop.getValue();
  74.   }
  75. }
  76.  
  77. class StackNode<T> {
  78.   private T value;
  79.   StackNode<T> next;
  80.  
  81.   public StackNode(T value) {
  82.     this.value = value;
  83.   }
  84.  
  85.   public void setNext(StackNode<T> next) {
  86.     this.next = next;
  87.   }
  88.  
  89.   public T getValue() {
  90.     return this.value;
  91.   }
  92.  
  93.   public StackNode<T> getNext() {
  94.     return this.next;
  95.   }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement