Class level locking with synchronization block, multiple threads on same instance

I have following code where multiple threads are invoking same instance method which is guarded by static object

package com.data;

class TestThread {

private final static Object object = new Object();

public void myWaitMethod() {
    synchronized (object) {
        System.out.println("Before a " + Thread.currentThread().getId());
        try {
            object.wait();
        } catch (Exception ignore) {
            System.out.println(Thread.currentThread().getId());
        }
        System.out.println("After a " + Thread.currentThread().getId());
    }
}

public void myNotifyMethod() {
    synchronized (object) {
        System.out.println("Before Notify");
        object.notifyAll();
        System.out.println("After b");
    }
}

public static void main(String[] args) throws InterruptedException {
    TestThread aa1 = new TestThread();
    new Thread(() -> aa1.myWaitMethod()).start();
    new Thread(() -> aa1.myWaitMethod()).start();
    Thread.sleep(1000);
    System.out.println("sleep");
    Thread.sleep(1000);
    aa1.myNotifyMethod();
}
}

o/p:

Before a 12
Before a 13
sleep
Before Notify
After b
After a 13
After a 12

I don’t understand why “Before a” twice printed even though the block is synchronized.

Answer

The first thread acquires the lock, prints Before A, the second thread now is blocked. Then the first thread calls wait (After calling wait, the thread loses control of the lock and enters the waiting state ), the second thread acquires the lock, prints Before A, and the second thread calls wait. (Both threads are in Waiting state, ). Then the main thread acquires the lock, then notify for both threads.

The diagram shown below represent various states of a thread at any instant of time.

enter image description here