When a thread is executing code from within a synchronized block, including any method code invoked from that synchronized block, the code is said to be executing in a synchronized context. The real question is, synchronized on what? Or, synchronized on which object's lock? When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you
specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object. Or you can synchronize on the current instance (this) as in the code above. Since that's the same instance that synchronized methods lock on, it means that you could always replace a synchronized method with a non-synchronized method containing a synchronized block.
May someone give me example for this part: , for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object.
There are many people that use Java to connect to databases, and do other work. There are many places where this would come up, but I suggest that as a beginner you just remember that code can synchronize of a variety of Objects.
Synchronization can lock if you try to do this:
Are you just studying, or do you have a real coding context for the question. [ October 03, 2007: Message edited by: Nicholas Jordan ]
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Joined: Sep 06, 2007
May you give me complete example for that? Thanks
Joined: Sep 17, 2006
It gets pretty involved, just keep going with what you have and get back with us if you have difficulties.