Managing Locks

Locks play a critical part in concurrent Java applications. In this section we’ll use lambda expressions to gain finer control over locks and open the doors to unit-test the proper locking of critical sections.

synchronized is an age-old keyword used to provide mutual exclusion. A synchronized block of code, such as synchronized { ... }, is a realization of the execute around method pattern. This pattern has been around since Java 1.0, but it was restricted and bound to the synchronized keyword in Java. Lambda expressions have now unleashed this pattern’s power.

synchronized has some shortcomings—see Java Concurrency in Practice [Goe06], by Brian Goetz, and Programming Concurrency on the JVM [Sub11]. First, it’s hard to time ...

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.