WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Advanced · question 41 of 100

What is the AQS (AbstractQueuedSynchronizer) in Java and how is it used?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

The AbstractQueuedSynchronizer (AQS) is a powerful framework provided by the java.util.concurrent package in Java. It is a synchronization tool that provides the building blocks for creating more complex synchronizers such as locks and semaphores.

AQS works by providing a FIFO queue of threads that are waiting to acquire a lock. When a thread tries to acquire the lock and it is already taken, it is added to the queue. When the lock is released, the thread at the head of the queue is notified and allowed to acquire the lock.

One of the key features of AQS is its extensibility. It provides two methods for synchronization: acquire() and release(). These methods can be overridden by subclasses to implement different synchronization patterns.

Here is an example of how AQS can be used to create a custom lock:

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class CustomLock {
    private static class Sync extends AbstractQueuedSynchronizer {
        @Override
        protected boolean tryAcquire(int acquires) {
            if (compareAndSetState(0, 1)) {
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            return false;
        }
    
        @Override
        protected boolean tryRelease(int releases) {
            if (getState() == 0) {
                throw new IllegalMonitorStateException();
            }
            setExclusiveOwnerThread(null);
            setState(0);
            return true;
        }
    
        @Override
        protected boolean isHeldExclusively() {
            return getState() == 1;
        }
    }
    
    private final Sync sync = new Sync();
    
    public void lock() {
        sync.acquire(1);
    }
    
    public boolean tryLock() {
        return sync.tryAcquire(1);
    }
    
    public void unlock() {
        sync.release(1);
    }
    
    public boolean isLocked() {
        return sync.isHeldExclusively();
    }
}

In this example, we create a CustomLock class that extends AQS. We override the tryAcquire(), tryRelease(), and isHeldExclusively() methods to define our custom locking behavior. We then use these methods to implement the lock(), tryLock(), unlock(), and isLocked() methods of our CustomLock class.

AQS can also be used to implement other synchronization primitives such as semaphores and barriers. Its flexibility and extensibility make it a powerful tool for building complex concurrent applications in Java.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics