Wait and notify concept is always linked to threads but they are methods in Object, not Thread. If you try to use them on a
Thread object you may not get the results you expect.
I will try to explain here-
Producer and Consumer is a classic example however to make it interesting consider following scenario below
We have a Bar Object and 3 working thread (2 bar tender and 1 helper)
Object Bar has 2 methods getGlass and putGlass and List of glasses. When a bar tender wants a glass it will call getGlass method of bar object and helper calls putGlass to place a glass on shelf.
Each time getGlass is called a glass is subtracted from list similarly each time put is called glass is added to List.
Now when list becomes empty and there are no glasses in the bar then any bar tender that calls getGlass would be asked to wait. And when helper thread calls putGlass then all bar tender thread gets notified as they are waiting. You can assume that the notifying mechanism is internal to restaurant(jvm
)
For your example, the right way would be-
hope it helps