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

Implementing a custom Lock class

Locks are one of the basic synchronization mechanisms provided by the Java concurrency API. It allows the programmers to protect a critical section of code, so only one thread can execute that block of code at a time. It provides the following two operations:

  • lock(): You call this operation when you want to access a critical section. If there is another thread running that critical section, other threads are blocked until they're woken up by the lock to get the access to the critical section.
  • unlock(): You call this operation at the end of the critical section, to allow other threads to access the critical section.

In the Java Concurrency API, locks are declared in the Lock interface and implemented in some classes, ...

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