By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
It isn't always important. But a synchronized block can be a "code smell" that warrants investigation. A block that synchronizes on an object X usually contains some operation on X. Assuming this block is sitting in another class Y it may be that it is in fact more tightly coupled with X than with Y. If that is so it probably wants to be refactored into X.Originally posted by Mehul Sanghvi:
Could you please make me understand the importance of having the code in the monitor object itself...
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Not compared to the very same chunk of code sitting in a methodOriginally posted by Jim Baiter:
But aren't there some benefits from using sync. blocks such as reducing the size of the critical section?
Absolutely. A couple of classes in the JFC use a monitor object, a new Object() created simply for its monitor lock. Take a look at java.io.File for example. Synchronized blocks are also necessary in other cases, e.g. an iteration over a (threadsafe) Collection must generally be synchronized on the Collection.Also you can use different objects to sync. on
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
originally posted by Peter Hann Den
Not compared to the very same chunk of code sitting in a method
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Don't get me started about those stupid light bulbs. |