This article is part of the Java Concurrent Programming series. If you want to learn more, check out the Java Concurrent Programming Overview.

preface

In the previous article. We learned about volatile, we learned about synchronized. Now let’s take a look at the locking capabilities implemented by the Lock interface (and related implementation classes) added since Java SE 5. Before reading this series, you are expected to be familiar with the principle of volatile and how CAS operates. If you are not familiar with or familiar with the two points mentioned above. So it is recommended to start from the design of the entire Java memory model and related knowledge points to understand, for details, please click -> “Java concurrent programming catalog”.

Design of a Concurrent package

To understand the locking capabilities that Java gives us based on the Lock interface (and related implementation classes), we first look at the design under the concurrent package as a whole. The specific design is as follows:

  • High level: Lock, synchronizer, blocking queue, etc.
  • Middle: AQS (AbstractQueuedSynchronizer), non-blocking data structure, the atomic variable classes.
  • Bottom layer: read/write of volatile variables, CAS operations.

Each layer of the dependency relationship is also very obvious, AQS, non-blocking data structure and the atomic variable classes (Java. Util. Concurrent. Atomic package’s classes), are based on the underlying implementation, and top class dependence on the middle these base classes. In particular, it is important to note that the locking functionality associated with the Lock interface (and the associated implementation classes) plays an important role in the overall hierarchy. Although the relationship of the Lock interface at a high level is not directly represented in the diagram, at a high level the synchronizers, blocking queues, concurrent containers, and so on that we have listed depend on or use locking functions implemented by their Lock interfaces (and related implementation classes) to a greater or less extent.

Therefore, understanding the Lock interface and related implementation classes is particularly important for understanding the overall mechanism and design of Java concurrency.

UML class diagrams of the Lock interface (and related implementation classes)

A Lock is used to control how multiple threads can access a shared resource. Generally, a Lock can prevent multiple threads from accessing a shared resource at the same time (but some locks can allow multiple threads to access a shared resource concurrently, such as read/write locks, which we will discuss later). Before the Lock interface, Java programs rely on synchronized keyword to achieve the Lock function, and after Java SE 5, and the package added Lock interface and related implementation classes, to achieve the Lock function. A UML class diagram for the Lock interface (and related implementation classes) looks like this:

As for the classes involved in the figure above, I have simply described them, without describing the methods in each class in detail. Because I think early we only need to understand the use of the relationship on the line. The classes involved and their use will be described in a subsequent article. (If you are reading on the mobile terminal, the picture may not be very clear, so it is recommended to read directly on the PC terminal.)

From the above figure, we find that the whole Lock interface and related implementation class relationship is relatively complex, so we are prepared to be introduced in the following parts:

  • Lock interface for Java concurrent programming locking mechanism
  • Concurrent Java programming of locking mechanism of AQS (AbstractQueuedSynchronizer)
  • LockSupport tool for Java concurrent programming locking mechanism
  • Condition interface for Java concurrent programming locking mechanism
  • Reentrant locking for Concurrent programming in Java
  • Read/write locking for Concurrent programming in Java

I believe you will be helpful after reading this series of articles. If you like me, don’t add me to wechat, just give me a “like”. Your support is the biggest encouragement to me. Hey ~ ~ ~ love you