import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class SharedCounter { int counter = 0; final Lock lock = new ReentrantLock(); final Condition counterReachedFive = lock.newCondition(); void incrementCounter() { lock.lock(); try { counter++; System.out.println("Counter incremented to: " + counter); if (counter == 5) { counterReachedFive.signalAll(); } } finally { lock.unlock(); } } void waitForCounterToReachFive() throws InterruptedException { lock.lock(); try { while (counter < 5) { System.out.println("Waiting for the counter to reach 5..."); counterReachedFive.await(); } System.out.println("The counter has reached 5!"); } finally { lock.unlock(); } } }