Managing Locks
Locks play a critical part in concurrent Java applications since they’re essential to ensure the correctness of changes to shared mutable variables from multiple threads. 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access