The position of the notify() call in the synchronized block does not matter, because by definition, if you are still in the synchronized block, you still hold the lock.
Should you notify the thread immediately when calling notification ()?
Yes. The notify() call puts one of the threads (if any) from the wait queue (waiting for the condition) to the blocked queue (waiting for the lock). This happens immediately, but the awakened thread must get a lock before it starts working. Therefore, it immediately moves out of the waiting queue, but is still waiting to receive a lock.
Btw, I would recommend writing this as this.wait() and this.notify() to be explicit about which object is affected.
source share