monitor mechanism in Java

monitor concept

The tube monitor. In the operating system, there is a semaphore and mutex, semaphore, and that is when the mutex, the mutex using basic development, need to be careful to use a mutex down and up, otherwise easily lead to deadlock. In order to better writing concurrent programs, the mutex and semaphore on the basis of a higher-level synchronization primitives, in fact, monitor belongs to the category of programming languages, C language does not support the monitor, and the monitor java support mechanism.

An important feature is that at the same time, only one thread / process can enter the critical region defined by the monitor, which makes it possible to monitor the effect of mutual exclusion. The process can not enter the critical section of the monitor / threads should be blocked, and wake up at the right time. Clearly, monitor as a synchronization tool, it should also provide a mechanism for such management thread / process.

monitor this mechanism is known as: higher level primitives, it inevitably need of external shielding these mechanisms, and implement these mechanisms within that monitor into a simple and easy excuse.

monitor basic elements

  • Critical section
  • monitor and lock objects
  • And a wait condition variables defined on a monitor object, signal operation

Use the monitor primarily for mutual exclusion to enter the critical zone, in order to be able to process blocked can not enter the critical section, the thread is required to assist a monitor object, this object will be a corresponding internal data structures such as lists, to save the blocked thread ; and because of the nature of monitor mechanism is based mutex primitive, it must maintain a lock mutex object-based.

In addition, in order to be able to wake up blocking and process / thread at the appropriate time, you also need to introduce a condition variable, the variable is used to determine when conditions are "appropriate time", this condition can be derived from the logic of the program code, it can be in monitor object interior, all in all, programmers have great autonomy to define the condition variable. However, due to monitor internal object uses a data structure to hold the queue is blocked, it must also provide external API to allow two thread into the blocked state and after being awakened, namely wait and notify.

monitor achieved in java

Delineation of critical areas

The modified method synchronized keyword, code blocks, is the critical area monitor mechanism

monitor object

In the above synchronized keyword is used, often need to specify an object associated with it, such as synchronized (this), in short, synchronized to manage an object, the object is to monitor object.

monitor mechanism, monitor I do not detect the problem serves as a wake-up blocking and maintenance mutex and wait, signalAPI to manage threads.

Java objects stored in memory, each divided into three parts, i.e., the object header and data alignment padding instance, the object in its head, holds a lock identifier; Meanwhile, java.lang.Object class defines the wait (), notify (), notifyAll () method, the specific implementation of these methods relies on a model called ObjectMonitor implemented, which is based on a mechanism inside the JVM implemented in C ++, the following basic principles:

 

 When a thread needs to acquire the lock Object, it will be placed in EntrySet wait, if the thread gets into the lock, the lock become the owner. If the logic according to the program, has received a lock thread missing some external conditions, and can not be continued (such as producers found the queue is full or consumers find the queue is empty), then the thread can wait method will lock by calling released into the wait set in obstruction to wait another thread at this time have the opportunity to acquire a lock, shop and go to other things, so that the external conditions are not satisfied prior to the establishment, which previously blocked thread can re-enter the EntrySet to compete lock. This condition is called external condition variable mechanism in the monitor.

Guess you like

Origin www.cnblogs.com/shemlo/p/11605681.html