This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Usman Shahir wrote:Steve , When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block until the first thread is done with the object.
we can do this by wait() , join() , yield() isn't it?
No. wait() will wait for a signal, not an open lock, and must be in a synchronized block. join() waits for a thread to complete, which is far different than waiting for a block of code to complete. And yield() offers up some of the current Thread's processor time if other Threads need to use it. These are all different than what synchronized blocks are for.
The fact that synchronization makes you wait on the lock is almost a necessary side effect. You don't use it because you want threads to block, you accept that other threads will block to protect the data the code is using.