Code example for AtomicBoolean

0
    } 
  } 
   
  // Taken straight from the LockSupport documentation 
  static class FIFOMutex { 
    private final AtomicBoolean locked = new AtomicBoolean(false);
    private final Queue<Thread> waiters = new ConcurrentLinkedQueue<Thread>();
 
    public void lock() { 
      boolean wasInterrupted = false;
      Thread current = Thread.currentThread();
      waiters.add(current);
 
      // Block while not first in queue or cannot acquire lock 
      while (waiters.peek() != current || !locked.compareAndSet(false, true)) {
        LockSupport.park(this);
        if(Thread.interrupted()) {
          System.out.println("Interrupted");
          wasInterrupted = true;
        } 
      }