O'Reilly logo

Java 7 Concurrency Cookbook by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required