What's a monitor in Java?

我怕爱的太早我们不能终老 提交于 2019-11-26 21:19:17

A monitor is mechanism to control concurrent access to an object.

This allows you to do:

Thread 1:

public void a()
{
    synchronized(someObject) {
        // do something (1)
    }
}

Thread 2:

public void b()
{
    synchronized(someObject) {
        // do something else (2)
    }
}

This prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time. One will start, and monitor will prevent the other from accessing the region before the first one finishes.

It's not a special object. It's synchronization mechanism placed at class hierarchy root: java.lang.Object.

There are also wait and notify methods that will also use object's monitor to communication among different threads.

A monitor is an entity that possesses both a lock and a wait set. In Java, any Object can serve as a monitor.

For a detailed explanation of how monitors work in Java, I recommend reading the Monitor Mechanics section of Concurrent Programming in Java (the preceding link displays the preview in Google books, and that section is available for reading).

mgibson
  1. A monitor is a concept/mechanism that's not limited to the Java Language;
  2. "In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread";
  3. As every reader knows, every object in Java is a sub-class of java.lang.Object. The java folks made java.lang.Object in such a way that it has features and characteristics that enables Java programmers to use any object as a monitor. For example, every object has a wait queue,a re-entrance queue and wait and notify methods making it a monitor;
  4. read about monitors here.

The Java language and runtime system support thread synchronization through the use of monitors.
A monitor is associated with a specific data item (a condition variable) and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data.

http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#33308

A mechanism to control access to objects one at a time

Monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true.

Monitors also have a mechanism for signaling other threads that their condition has been met. It is an entity that possesses both a lock and a wait set. In Java, any Object can serve as a monitor.

In the Java virtual machine, every object and class is logically associated with a monitor. To implement the mutual exclusion capability of monitors, a lock (sometimes called a mutex) is associated with each object and class. This is called a semaphore in operating systems terms, mutex is a binary semaphore.

For more information check the link

http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/monitors.html

monitor is associate with object or data member, which is acquire when a data member or object is enter is synchronization block(critical section) and release when is exit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!