Code example for AtomicReference

0
    } 
 
    private class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 1L;
 
        private final AtomicReference<Object> permitHolder = new AtomicReference<>(null);
 
        @Override 
        protected synchronized boolean tryAcquire(int permit) {
            if (compareAndSetState(0, 1)) { 
                permitHolder.set(permit);
                return true; 
            } else if (permitHolder.get().equals(permit)) {
                for (;;) { 
                    int current = getState();
                    int next = current + 1; // increase by one
                    if (next < 0) // overflow
                        throw new Error("Maximum lock count exceeded");
                    if (compareAndSetState(current, next)) {
                        return true; 
Contextual code suggestions in your IDE  Get Codota for Java