Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, March 6, 2017

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

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


When reading articles about Java threads, I often notice the expression: "current thread is the owner of this object's monitor". I get the meaning: the thread gets the right to operate on the object. But I am puzzled why we use the phrase "the object's monitor" instead of "the object's lock"?

In brief, I don't know the meaning of the word 'monitor' The question may be strange and simple. But I wish anybody can help to solve it. 3ks

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


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

There's a great Wikipedia article on Monitors:

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

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

Answer by Stephen C for 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.

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.

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


Every object has some sort of Monitor built into it, waiting for it to be used by some code. In reality, most objects are never used as a monitor, so the monitors don't have to be created until they are used. Rather than implementing this feature as every object having a private Monitor monitor field, think of it as being implemented as the JVM having a global HashMap monitors.

A possible implementation is this: Whenever a synchronized block is entered, the JVM looks up the synchronized object in the map (monitors). If it finds it, it gets the monitor to use. If it doesn't find it, it enters a critical section dedicated to the map. It then looks up the object again because another thread may have created it between the previous check and entering the critical section. If it's still not there, it creates the monitor for the synchronized object and leaves the critical section

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


A synchronized block around an object is its monitor, which controls a lock on the object. Here an example

synchronized (object) {     while ()        object.wait(timeout);     ... // Perform action appropriate to condition  }  

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


The Java Virtual Machine uses monitors to support multithreading. Monitors achieve this through two concepts - Mutual exclusion while running the threads (here is where 'locking' comes into picture) and coordination as a means of inter thread communication (here is where object's wait and notify methods come into picture).

Reading the following part from "Inside JVM" will clear this doubt, is it very nicely explained over here (Chapter 20, Thread synchronization) -

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

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


Even though it is late to answer this question, I thought to just add in-case it is useful.
Here is a synchronized block of Java code inside an unsynchronized Java method

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

In the example "this" is used, which is the instance the add method is called on. A synchronized instance method uses the object it belongs to as monitor object.
=> Only one thread can execute inside a Java code block synchronized on the same monitor object.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.