Replaced call to Thread.sleep with wait/notifyAll.