Advertisement
Guest User

universal-dich

a guest
Nov 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. import kotlinx.atomicfu.AtomicRef;
  2.  
  3. public class Solution implements AtomicCounter {
  4.     private final ThreadLocal<Node> last;
  5.  
  6.     public Solution(){
  7.         this.last = new ThreadLocal<>();
  8.     }
  9.  
  10.     public int getAndAdd(int x) {
  11.         int res;
  12.         while(true){
  13.             int old = last.get().val.getValue();
  14.             int upd = old;
  15.             res = upd + x;
  16.             ThreadLocal<Node> node = new ThreadLocal<>();
  17.             node.get().val.setValue(upd);
  18.             last.set(last.get().next.decide(node.get()));
  19.             if (last.get() != node.get()){
  20.                 break;
  21.             }
  22.         }
  23.         return res;
  24.     }
  25.  
  26.     private static class Node {
  27.         private final AtomicRef<Integer> val = new AtomicRef<>(0);
  28.         private final Consensus<Node> next = new Consensus<>();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement