The basic concept of synchronization in Java monitor-the simplest and crudest way to understand

There is a course called Operating System in the university. Students who have studied it should remember that the monitor is an important basic concept for the synchronization of the operating system. It is also used in the thread synchronization of JAVA. This article uses an analogy to explain monitoring. "Monitor".

1. What is a monitor

The monitor can be regarded as a specially arranged building. This building has a special room that usually contains some data and codes, but only one consumer (thread) can use this room at a time.

Insert picture description here When a consumer (thread) uses this room, he must first go to a lobby (Entry Set) to wait. The scheduler will select a consumer (thread) from the lobby based on certain criteria (eg FIFO) and enter the special room If this thread is "suspended" for some reason, it will be scheduled to the "waiting room" by the scheduler, and will be reassigned to a special room after a period of time. According to the above line, this building contains three rooms , Respectively are "special room", "hall" and "waiting room".

Insert picture description here Simply put, the monitor is used to monitor threads entering this special room. It ensures that only one thread can access the data and code in the special room at a time.

2. Realization of monitor in JAVA

In the JAVA virtual machine, each object (Object and class) is associated with the monitor through a certain logic. In order to realize the mutual exclusion function of the monitor, each object (Object and class) is associated with a lock (sometimes called "mutual Exclusion"), this lock is called "semaphore" in operating system books, mutual exclusion ("mutex") is a binary semaphore.

If a thread holds a lock for some data, other threads cannot acquire the lock until the thread releases the lock. In multithreading, if we write this semaphore by ourselves at any time, it is obviously not very convenient. Fortunately, JVM automatically implements this for us.

In order to prevent the data from being accessed by multiple threads, Java provides two implementations: synchronization block and synchronization method. Once a piece of code is embedded in a synchronized keyword, it means that it is placed in the monitoring area, and the JVM will automatically provide this segment in the background. The code implements the lock function.

3. Which part of the JAVA synchronization code is the monitor?

We know that each object (Object/class) of JAVA is associated with a monitor. It is better to say that each object (Object/class) has a monitor. The object can have its own critical section and can monitor threads. Sequence In order to make threads cooperate, JAVA provides wait() and notifyAll and notify() to suspend the thread and wake up another waiting thread. In addition, there are three different versions of these methods:

wait(long timeout, int nanos)
wait(long timeout) notified by other threads or notified by timeout.
notify(all)

These methods can only be called in a synchronized block or synchronized method. The reason is that if a method does not need to be mutually exclusive, does not require monitoring or collaboration between threads, and each thread can freely access this method, there is no need for collaboration.

At last

Students who want to learn java can reply to the information by private message to receive a summary of the Java interview questions of the first-line manufacturers + Alibaba Taishan manual + learning and thinking guide for each knowledge point + a summary of Java core knowledge points in a 300-page pdf document!

The content of these materials are all the knowledge points that the interviewer must ask during the interview. The chapter includes many knowledge points, including basic knowledge, Java collections, JVM, multi-threaded concurrency, spring principles, microservices, Netty and RPC, Kafka , Diary, design pattern, Java algorithm, database, Zookeeper, distributed cache, data structure, etc.

file

Guess you like

Origin blog.csdn.net/weixin_46577306/article/details/108041674