Konrad Reiche
Konrad Reiche

Reputation: 29543

Terms to distinguish Java programming language monitor and wait/notify monitors

I am having issues with the terms for the Java monitors. First of all I distinguish between two kinds of monitors:

The monitor itself is an object, on which wait() or synchronized is called on. How can these two types of monitors be distinguished? In the JVMTI API they write:

"monitor" means Java programming language object monitor.

This does not help me. However, further they use the terms Monitor and Contended Monitor to distinguish them at least in the API in terms of function calls.

Upvotes: 0

Views: 170

Answers (3)

Stephen C
Stephen C

Reputation: 719346

Fundamentally, these are the same thing. They are variously called monitors, mutexes and (in Java) primitive locks, and these terms mean pretty much the same thing in current usage.

The primary usage pattern involving a mutex with threads calling wait() and notify() is referred to as a condition, but the same mutex can simultaneously be used as a simple region of mutual exclusion.

The phrase "contended monitor" refers to a monitor / mutex / primitive lock where there is contention over the region of mutual exclusion. Or to put it more simply, where one thread holds the mutex, and others are waiting to acquire it.

Wikipedia references:

(It should be noted that these pages are not definitive, and not entirely consistent. But then most IT folks play "fast and loose" with terminology, and very few people have read the original publications. But hey ... that's how language evolves.)

Upvotes: 3

finnw
finnw

Reputation: 48659

They are both the same thing. The "monitor" provides both functions. Notice that the VM instructions for a synchronized block are called monitorenter and monitorexit

Contention can happen on monitorenter (i.e. entering the synchronized block) or wait (which attempts to re-acquire the lock when it awakens.)

By contrast, the Lock and Condition interfaces from java.util.concurrent are presented as separate objects. They behave similarly to monitors though (Condition.await() re-acquires the lock and may need to contend with a Lock.lock() by another thread.)

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346457

You can not acquire a monitor by using wait(). Quite the opposite: you first have to acquire the monitor using synchronized before you can use wait() on it, otherwise you will get an IllegalMonitorStateException.

So the distinction you describe does not exist.

Upvotes: 1

Related Questions