#477 Code Review of Waiting for a Condition with Lock and Condition

Companies:
Alfa-Bank
Themes:
JavaConcurrencyCode Review

Perform a review: is everything correct here?

Java
class Scratch {
    private static final Logger log = getLogger(Scratch.class);
    private static volatile boolean ready = false;
    private static final Lock rLock = new ReentrantLock();
    private static final Condition readyCondition = rLock.newCondition();

    private static void waitAndLog() {

        try {
            rLock.lock();
            log.info("rLock acquired, ready: {}", ready);
            if (!ready)
                readyCondition.await();
            log.info("ready was awaited: {}", ready);
        }
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        finally {
            rLock.unlock();
        }
    }

    public static void main(String[] args) {
        new Thread(Scratch::waitAndLog).start();

        try {
            rLock.lock();
            ready = true;
            log.info("signal about ready");
            readyCondition.signal();
        }
        finally {
            rLock.unlock();
        }
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in