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

Java Concurrency · Intermediate · question 26 of 100

What are the different types of locks available in Java and when would you use them?

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

In Java, there are several types of locks available that can be used to synchronize access to shared resources and prevent race conditions. Here are some of the most common types of locks in Java:

ReentrantLock - A ReentrantLock is a type of lock that can be acquired and released multiple times by the same thread. This can be useful in situations where a thread needs to access a shared resource multiple times, and the resource can only be accessed by one thread at a time.

Lock lock = new ReentrantLock();

lock.lock();
try {
    // do something
} finally {
    lock.unlock();
}

ReadWriteLock - A ReadWriteLock is a type of lock that allows multiple threads to read a shared resource at the same time, but only one thread to write to the resource at a time. This can be useful in situations where a resource is read more often than it is written.

ReadWriteLock lock = new ReentrantReadWriteLock();

// Read lock
lock.readLock().lock();
try {
    // read resource
} finally {
    lock.readLock().unlock();
}

// Write lock
lock.writeLock().lock();
try {
    // write resource
} finally {
    lock.writeLock().unlock();
}

StampedLock - A StampedLock is a type of lock that is similar to a ReadWriteLock, but with more advanced features. It supports optimistic locking, which allows multiple threads to read and write to a shared resource at the same time, as long as there are no conflicting writes.

StampedLock lock = new StampedLock();

// Read lock
long stamp = lock.tryOptimisticRead();

// read resource
if (!lock.validate(stamp)) {
    // upgrade to a full read lock
    stamp = lock.readLock();
    try {
        // read resource
    } finally {
        lock.unlockRead(stamp);
    }
}

// Write lock
long stamp = lock.writeLock();
try {
    // write resource
} finally {
    lock.unlockWrite(stamp);
}

Semaphore - A Semaphore is a type of lock that allows a fixed number of threads to access a shared resource at the same time. This can be useful in situations where a resource can only be accessed by a limited number of threads at a time.

Semaphore semaphore = new Semaphore(10);

semaphore.acquire();
try {
    // access shared resource
} finally {
    semaphore.release();
}

Condition - A Condition is a type of lock that allows threads to wait for a specific condition to be met before proceeding. This can be useful in situations where a thread needs to wait for a certain event to occur before proceeding.

Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();

// Thread 1
lock.lock();
try {
    while (conditionNotMet) {
        condition.await();
    }
    // do something
} finally {
    lock.unlock();
}

// Thread 2
lock.lock();
try {
    conditionMet = true;
    condition.signal();
} finally {
    lock.unlock();
}

In summary, the different types of locks available in Java can be used in different situations depending on the requirements of the shared resource being accessed. Choosing the right type of lock can be critical for ensuring that a multi-threaded application behaves correctly and efficiently.

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