Advertisement
Jaydeep999997

Non Blocking Stack - Customer CAS

Nov 2nd, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 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.  
  7. class Demonstration {
  8.   public static void main(String[] args) throws Exception {
  9.     Stack<Integer> stack = new Stack<Integer>();
  10.     ExecutorService executor = Executors.newFixedThreadPool(20);
  11.     int numThreads = 2;
  12.     CyclicBarrier barrier = new CyclicBarrier(numThreads);
  13.     try {
  14.       for (int i = 0; i < numThreads; i++) {
  15.         executor.submit(
  16.             new Runnable() {
  17.               @Override
  18.               public void run() {
  19.                 for (int j = 0; j < 1000; j++) {
  20.                   stack.push(j);
  21.                 }
  22.                 try {
  23.                   barrier.await();
  24.                 } catch (InterruptedException e) {
  25.  
  26.                 } catch (BrokenBarrierException e) {
  27.  
  28.                 }
  29.  
  30.                 for (int j = 0; j < 1000; j++) {
  31.                   stack.pop();
  32.                 }
  33.               }
  34.             });
  35.       }
  36.     } finally {
  37.       executor.shutdown();
  38.       executor.awaitTermination(10, TimeUnit.SECONDS);
  39.     }
  40.   }
  41. }
  42.  
  43. class Stack<T> {
  44.   private SimulatedCompareAndSwap<StackNode<T>> topNode =
  45.       new SimulatedCompareAndSwap<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.getValue();
  53.       if (currentTop != null) {
  54.         node.setNext(currentTop);
  55.       }
  56.     } while (topNode.compareAndSwap(currentTop, node));
  57.   }
  58.  
  59.   public T pop() {
  60.     if (topNode.getValue() == null) {
  61.       return null;
  62.     }
  63.     StackNode<T> currentTop;
  64.     StackNode<T> newTop;
  65.     do {
  66.       currentTop = topNode.getValue();
  67.       if (currentTop == null) {
  68.         return null;
  69.       }
  70.       newTop = currentTop.getNext();
  71.     } while (topNode.compareAndSwap(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.  
  98. class SimulatedCompareAndSwap<T> {
  99.   private T value;
  100.  
  101.   public SimulatedCompareAndSwap(T value) {
  102.     this.value = value;
  103.   }
  104.  
  105.   synchronized T getValue() {
  106.     return this.value;
  107.   }
  108.  
  109.   public synchronized boolean compareAndSwap(T expected, T newValue) {
  110.     if (expected == null && value == null) {
  111.       value = newValue;
  112.       return true;
  113.     }
  114.     if (!expected.equals(value)) {
  115.       return false;
  116.     }
  117.     this.value = newValue;
  118.     return true;
  119.   }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement