I am writing a Multi threaded program to print numbers from 1 to n.
I have 2 threads which has a runner which prints Odd
number. And 1 thread which has a runner which prints Even
number.
while (true) {
synchronized (ng) {
while (ng.getData() % 2 == 1) {
try {
ng.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ng.increment();
ng.notify();
}
I have put debug points inside synchronized method. Attaching the snapshot:
After the 1st thread called
notify()
, in the debug tab, it still shows
owns NumberGenerator
You can see in the snapshot:
It says 2 threads: Thread-1 and Thread-2 owns NumberGenerator object.
How can 2 threads hold a lock on object at same time?