What's a Monitor in Java

What's a monitor in Java?

A monitor is mechanism to control concurrent access to an object.

This allows you to do:

Thread 1:

public void a()
{
synchronized(someObject) {
// do something (1)
}
}

Thread 2:

public void b()
{
synchronized(someObject) {
// do something else (2)
}
}

This prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time. One will start, and monitor will prevent the other from accessing the region before the first one finishes.

It's not a special object. It's synchronization mechanism placed at class hierarchy root: java.lang.Object.

There are also wait and notify methods that will also use object's monitor to communication among different threads.

What is a `monitor` in JVM(Hotspot), a specific object?

A monitor is a concept on which you can perform certain operations. Anything that implements the abstract operations of the concept of a monitor is a good implementation.

The concept is implemented in HotSpot in the mark word plus everything described in the text that you quoted about the mark word. It is not a single data structure.

In Java, what is the difference between a monitor and a lock

From the official documentation of Locks and Synchronization:

  • Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. (The API specification often refers to this entity simply as a "monitor.")
  • Every object has an intrinsic lock associated with it. By convention, a thread has to acquire the object's monitor lock
    before accessing them, and then release the monitor lock when it's
    done with them. A thread is said to own the lock between the time it
    has acquired the lock and released the lock. As long as a thread owns
    a monitor lock, no other thread can acquire the same lock. The other
    thread will block when it attempts to acquire the lock.
  • When a thread releases the lock, a happens-before relationship is established between that action and any subsequent acquisition of the
    same lock.

So a monitor and a lock can not be compared for differences, rather they are complementary to each other. Every object in Java is associated with a monitor which a thread can lock or unlock.

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

but I am puzzled why use word "the object's monitor" instend of "the object's 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 C. A. R. Hoare, and were first implemented in Brinch Hansen's Concurrent Pascal language."

(Source: Wikipedia)

Why use the term "monitor" rather than "lock"? Well strictly speaking, the terms do mean different things ... especially if you use them in the way that they were originally intended to be used.

  • A "lock" is something with acquire and release primitives that maintain certain lock properties; e.g. exclusive use or single writer / multiple reader.

  • A "monitor" is a mechanism that ensures that only one thread can be executing a given section (or sections) of code at any given time. This can be implemented using a lock (and "condition variables" that allow threads to wait for or send notifications to other threads that the condition is fulfilled), but it is more than just a lock. Indeed, in the Java case, the actual lock used by a monitor is not directly accessible. (You just can't say "Object.lock()" to prevent other threads from acquiring it ... like you can with a Java Lock instance.)

In short, if one were to be pedantic "monitor" is actually a better term than "lock" for characterizing what Java is providing. But in practice, both terms are used almost interchangeably.

What does it mean when someone says - a thread enters an object's monitor?

Each object, not only synchronized.

JLS chapter 17

The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.

java monitor - queues

In the case of the code you posted, since the methods are not static methods, the monitor is associated with the object, not the class: there is one such monitor for each instance of the class.

There is one queue of waiting threads for each instance of this class. The queue applies to both the synchronized methods, so if one thread is executing one of those methods, no other thread can execute either of those methods.

Every object and every class in Java has its own built in monitor. The object monitors apply to nonstatic methods, and the class monitors apply to static methods. The monitors are part of the language definition, and do not have to be explicitly declared or defined.



Related Topics



Leave a reply



Submit