views:

553

answers:

3

Each Java object (and its class) has an associated monitor. In pthread terms a Java monitor is equivalent to the combination of a reentrant mutex and a condition variable.

For locking, the Win32 API provides Mutex objects (which are reentrant but heavyweight) and Critical Sections (which are non-reentrant but lightweight). It also provides other synchronization constructs such as Semaphores and Events but has no explicit concept of a condition variable.

If I were writing a JVM, how could I use these Win32 concepts to implement Java monitors?

A: 

I suggest you take a look at the OpenJDK source to see how the class ReentrantLock was implemented.

(I haven't checked it myself so i'm not sure of the answer).

the util.concurrent locks are implemented using native API.

Shimi Bandiel
On windows it is implemented using native API and not Java monitors.
Shimi Bandiel
I will take a look. Thanks.
Matthew Murdoch
I'm struggling to find the relevant source code. Could you point me in the right direction?
Matthew Murdoch
+1  A: 

Windows has SignalObjectAndWait() which can be used very much like a wait on a condition variable in a monitor. You can use an Event (that is Reset) and a Mutex and then use PulseEvent() to do the equivalent of signalling the condition variable.

Adisak