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.
