Himanshu Ahire
Himanshu Ahire

Reputation: 717

How is an Object linked with a Thread so wait() and notify() work

In Java the Object and Thread are separate classes. The Java Object source code doesn't mention anything about monitor / Thread although we have the wait() /notify() methods.

So the question is how does the Object's monitor knows details of thread which has acquired it ? Where is this information stored ? how are the objects and threads linked ?

Upvotes: 3

Views: 101

Answers (1)

RealSkeptic
RealSkeptic

Reputation: 34638

Generally speaking, this is implementation-dependent.

The Java code for both Object and Thread only does rather superficial stuff. But the real work is done by native methods.

The Java Language Specification specifies how an object monitor and its wait sets are supposed to behave, but it does not specify how this is going to be implemented.

Furthermore, although the Java Virtual Machine has the commands monitorenter and monitorexit, the JVM spec says:

The association of a monitor with an object may be managed in various ways that are beyond the scope of this specification. For instance, the monitor may be allocated and deallocated at the same time as the object. Alternatively, it may be dynamically allocated at the time when a thread attempts to gain exclusive access to the object and freed at some later time when no thread remains in the monitor for the object.

The synchronization constructs of the Java programming language require support for operations on monitors besides entry and exit. These include waiting on a monitor (Object.wait) and notifying other threads waiting on a monitor (Object.notifyAll and Object.notify). These operations are supported in the standard package java.lang supplied with the Java Virtual Machine. No explicit support for these operations appears in the instruction set of the Java Virtual Machine

This all amounts to one thing: How exactly an object is implemented, what's in its header, how its monitor is implemented, and how the wait() and notify() methods are implemented are all entirely up to the programmers who wrote the particular JVM. The implementation by Oracle (inherited from Sun) may be completely different than that of IBM or IcedTea.

Furthermore, even for the same programming team, the details of implementations differ between various operating systems for which the JVM is available. The reason for this is clear: the JVM depends on the operating system's threading mechanism to implement its threads, and each operating system offers access to threads in very different ways.

So a programmer who writes a threading implementation for Windows does very different things than one who writes a threading implementation for Linux or MacOS X. The threads may be locked in different ways, so monitors may be completely different between the operating system.

To sum up:

  • The way the monitor of an object and the waiting sets for that monitor behave is defined in the Java Language Specification.
  • Thread management is operating-system dependent. Therefore, it has to be implemented in the native level, not in the Java language itself.
  • The data structures required to hold the monitor, the current thread that holds it, the waiting sets etc. are also all on the native level, and may be different between operating systems and between JVM implementations.
  • It's not necessarily true that the way Oracle implemented all this in its own JVM is the only right way and is followed by all other implementations.

Upvotes: 7

Related Questions