Cover by Javier Fernández González

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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 synchronized keyword, you have to get and free the control over a synchronized block of code in a structured way. The Lock interfaces allow you to get more complex structures to implement your critical section.
  • The Lock interfaces provide additional functionalities over the synchronized keyword. One of the new functionalities is implemented ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required