You are synchronizing on monitor
, so you should wait()
on monitor, too:
monitor.wait();
Right now you are waiting on this
, which is not the owner of the monitor because synchronization is on monitor
.
Note that of course the notify
should also be done on the monitor
object, and that you might want to consider using notify
/notifyAll
in both threads. Otherwise it may happen that one thread starves waiting for a missing notification. Using a timeout (the overloaded version of wait
) might also be a good idea to catch corner cases.