Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlinx.atomicfu.AtomicRef;
- public class Solution implements AtomicCounter {
- private final ThreadLocal<Node> last;
- public Solution(){
- this.last = new ThreadLocal<>();
- }
- public int getAndAdd(int x) {
- int res;
- while(true){
- int old = last.get().val.getValue();
- int upd = old;
- res = upd + x;
- ThreadLocal<Node> node = new ThreadLocal<>();
- node.get().val.setValue(upd);
- last.set(last.get().next.decide(node.get()));
- if (last.get() != node.get()){
- break;
- }
- }
- return res;
- }
- private static class Node {
- private final AtomicRef<Integer> val = new AtomicRef<>(0);
- private final Consensus<Node> next = new Consensus<>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement