What is the meaning of an object monitor in Java? Why use this word?

When reading articles about Java threads, I often notice the expression: "The current thread is the owner of this object monitor." I get the value: the thread gets the right to work with the object. But I am puzzled, why do we use the phrase ā€œobject monitorā€ instead of ā€œobject lockā€?

In short, I don’t know the meaning of the word ā€œmonitorā€. The question can be strange and simple. But I want someone to help solve it. 3KS

+73
java multithreading monitor
Mar 24 '12 at 2:23
source share
7 answers

but I'm puzzled, why use the word "object monitor" instead of "object lock"?

See ulmangt's answer for links that explain the term ā€œmonitorā€ as used in this context. Note that:

"Monitors were invented by Per Brinch Hansen and CAR Hoare, and were first implemented in the parallel language Pascal Brinch Hansen."

(Source: Wikipedia )

Why use the term ā€œmonitorā€ rather than ā€œlockā€? Strictly speaking, terms mean different things ... especially if you use them the way they were originally intended to be used.

  • A "lock" is something with primitives of receipt and release that support certain properties of the lock; for example, exclusive use or one writer / several readers.

  • A ā€œmonitorā€ is a mechanism that ensures that only one thread can execute a given section (or sections) of code at any given time. This can be accomplished with a lock (and ā€œcondition variablesā€ that allow threads to wait or send notifications to other threads about the condition to be met), but this is more than just a lock. Indeed, in the case of Java, the actual lock used by the monitor is not directly accessible. (You simply cannot say "Object.lock ()" so that other threads cannot get it ... as you can with an instance of Java Lock .)

In short, if you want to be pedantic, ā€œmonitorā€ is actually a better term than ā€œblockingā€ to characterize what Java provides. But in practice, both terms are used almost interchangeably.

+45
Mar 24 2018-12-12T00:
source share

A monitor is simply a term for an object whose methods can be safely used in a multi-threaded environment.

There's a great Wikipedia article about monitors:

http://en.wikipedia.org/wiki/Monitor_ (sync)

If you scroll down, it even got a section explicitly about Java .

+20
Mar 24 '12 at 2:35
source share

A synchronized block around an object is its monitor, which controls the locking of an object. Here is an example

 synchronized (object) { while (<condition does not hold>) object.wait(timeout); ... // Perform action appropriate to condition } 
+5
Jul 08 '14 at 20:28
source share

Each object has a built-in monitor, expecting it to be used by some kind of code. In fact, most objects are never used as a monitor, so monitors should not be created until they are used. Instead of implementing this function, since each object has its own monitor monitor field, think of it as an implementation as a JVM with global HashMap monitors.

A possible implementation is this: whenever a synchronized block is entered, the JVM looks at the synchronized object on the map (monitors). If he finds it, he will get a monitor. If he does not find it, he enters the critical section on the map. Then it again searches for the object, because another thread can create it between the previous scan and entering the critical section. If it does not already exist, it creates a monitor for the synchronized object and leaves a critical section

+4
Mar 24 '12 at 3:30
source share

Although it’s too late to answer this question, I thought it’s easy to add in case it’s useful.
Here is a synchronized block of Java code inside an unsynchronized Java method

 public void add(int value){ synchronized(this){ this.count += value; } } 

The example uses "this", which is the instance on which the add method is called. The synchronized instance method uses the object to which it belongs as a monitor object.
=> Only one thread can be executed inside a block of Java code synchronized on one monitor object.

+4
Feb 02 '17 at 17:32
source share

The Java virtual machine uses monitors to support multithreading. Monitors achieve this using two concepts: mutual exclusion at the start of flows (here, "locking" occurs) and coordination as a means of cross-thread communication (there are methods for waiting for an object and notification).

Reading the next part from the Inside JVM will remove this doubt, it is very well explained here (chapter 20, thread synchronization) -

https://www.artima.com/insidejvm/ed2/threadsynchP.html

+2
Aug 12 '15 at 5:24
source share

Quote from inside a Java Virtual Machine

A thread in a Java virtual machine requests a lock when it arrives at the beginning of the monitor area. In Java, there are two kinds of monitor areas: synchronized statements and synchronized methods.

monitor

The monitor is similar to a building in which there is one special room in which only one thread can be at a time. A room usually contains some data. From the time a stream enters this room until the time it leaves, it has exclusive access to any data in this room. The entrance to the monitor building is called the "entrance to the monitor." Entrance to a special room inside the building is called a ā€œmonitor acquisitionā€. Occupation of a room is called ā€œmonitor ownershipā€, and leaving the room is called ā€œmonitor releaseā€. Exiting the entire building is called "exiting the monitor."

In addition to being associated with a data bit, a monitor is associated with one or more bits of code, which in this book will be called areas of the monitor.

As mentioned earlier, the language provides two built-in ways to define areas of the monitor in your programs: synchronized statements and synchronized methods. These two mechanisms that implement the mutual exclusion aspect of synchronization are supported by the Java virtual machine instruction set.

Castle

To realize the possibility of mutually excluding monitors, the Java virtual machine associates a lock (sometimes called a mutex) with each object and class. Blocking is like a privilege that only one thread can own at a time.

One thread is allowed to block the same object several times. For each object, the Java virtual machine counts the number of times the object was locked. An open object has a zero counter. When the thread receives the lock for the first time, the count is again increased to one. Each time a thread receives a lock for the same object, the count is increased again.

+2
Feb 15 '19 at 6:15
source share



All Articles