Hi
Imagine an object is as a building. Its methods are rooms. Methods or blocks in the methods, as You know, can be either synchronized or not synchronized. Imagine, that the synchronized methods and blocks are rooms with a locked door and the unsynchronized ones are rooms with an opened door.
Every object has a lock (only one), which is like a key, which can open the closed doors. When a
Thread would like to enter a closed room, it needs the key. And the big truth is, that there is only one key, so only one Thread can execute the synchronized methods.
Let us have two threads A and B. If A has a key (owns the lock), thread B has no chance to enter the closed rooms (synchronized methods) and it has to wait - it gets to the "blocked state".
At this moment thread A has the key. In the room, it noticed, that it can not carry on in its work (for example the temperature in the room is too high) so it execute the object's wait() method. The result is, that thread A gives back the key to the object and the object puts thread A to a pool, where thread A can wait.
At this moment the key is free and any thread can borrow it. For example thread B, which has been in the blocked state, got the key and know can enter the closed room (synchronized methods and blocks). It adjusts the termostat (to a lower temperature) and it knows, that it have to inform threads, which are in the waiting pool, so it calls the object's notify() (a random thread from the pool is informed) or the notifyAll() (every thread in the pool is informed) method. Thread B still owns the key as it is in a synchronized room! Thread A has notified (informed), that there is a different temperature now, so it would like to go back to the room he left, when it had called the object's wait() method. But the key is not free (thread B owns it), so thread A gets to the blocked state.
Thread B still can do some stuff in the synchronized rooms. when it exit the closed room, it gives back the key to the object, so another threads which are in blocked state, get chance to enter closed rooms (synchronized methods or blocks). For example thread A
and it can finish its work, as the temperature is lower now.
[ May 08, 2004: Message edited by: Gabriel Forro ]