Synchronizing a block of code with a Lock
Java provides another mechanism for the synchronization of blocks of code. It's a more powerful and flexible mechanism than the
synchronized keyword. It's based on the
Lock interface and classes that implement it (as
ReentrantLock). This mechanism presents some advantages, which are as follows:
- It allows the structuring of synchronized blocks in a more flexible way. With the
synchronizedkeyword, you have to get and free the control over a synchronized block of code in a structured way. The
Lockinterfaces allow you to get more complex structures to implement your critical section.
Lockinterfaces provide additional functionalities over the
synchronizedkeyword. One of the new functionalities is implemented ...