According to the documentation for ReentrantLock.newCondition(), the calling thread needs to own a lock before calling a signaling method:
If this lock is not held when any of the Condition waiting or signalling methods are called, then an IllegalMonitorStateException is thrown.
Indeed, this is what I see when I try it:
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signalAll(AbstractQueuedSynchronizer.java:1954)
at PingPongPrinter_.printPing(PingPong2.java:35)
at WorkerPing.run(PingPong2.java:53)
at java.lang.Thread.run(Thread.java:748)
So why do this restriction exists in java.util.concurrent.locks.ReentrantLock
? I believe there is no such restriction in C++.