your synchronization is definitely broken. Before calling wait() and notify() on some object you must obtain the lock for this object. In Timer you're synchronizing on this and then calling this.notifyAll() (everything's fine), but in Timertest you're synchronizing on this and calling timer.wait() - and I guess you see an exception.
I don't get an error, but the Thread waits forever.
Yup, it does indeed. The problem is that run() method in Timer is synchronized, which means it holds the lock all the time and never releases it, so TimerTest never has a chance to get it, so it just waits. Instead of synchronizing the whole method you have to narrow the synchronized block, so the Timer thread would release the lock from time to time.
What you need is something like this (and remove synchronized from method declaration)
Well done about putting sleep outside synchronized - I've put it wrong.
PS. there's no need to declare run() as synchronized in TestTimer. You don't really want to use more locks than you need, otherwise one day you may end up in a deadlock
Destroy anything that stands in your way. Except this tiny ad: