JAVA -- multi-thread programming synchronous "monitor" (3), multi-thread programming monitor
- Reprinted from:
- Http://www.programcreek.com/2011/12/monitors-java-synchronization-mechanism/
- Http://ifeve.com/monitors-java-synchronization-mechanism/
- The monitor is an important basic concept for implementing synchronization in the operating system. It is also used in JAVA thread synchronization. This article explains the monitor "monitor" with a type of push ".
[1] What is a monitor?
- The monitor can be viewed as a specially arranged building. This building has a special room which usually contains some data and code, but can only be used by one consumer (thread) at a time.
- When a consumer (thread) uses this room, first he has to wait in a lobby (Entry Set), The scheduler will be based on certain criteria (e.g. FIFO will select a consumer (thread) from the lobby to enter a special room. If the thread is "suspended" For some reason, it will be scheduled by the scheduler to "Wait For the room ", after a period of time, the building will be reassigned to a special room. According to the above line, the building contains three rooms: "special room", "Hall", and "waiting for room ".
- Simply put, the monitor monitors threads entering this particular room. It ensures that only one thread can access the data and code in the special room at a time.
[2] monitor implementation in JAVA
- In the Java Virtual Machine, each Object (Object and class) is associated with the monitor through a logic. To achieve the mutex function of the monitor, each Object (Object and class) A lock (sometimes called mutex) is associated. This lock is called a "semaphore" in operating system books. mutex is a binary semaphore.
- If a thread has a lock for some data, other threads cannot obtain the lock until the thread releases the lock. In multithreading, if we write this semaphore by ourselves at any time, it is obviously not very convenient. Fortunately, JVM automatically implements this for us.
- To prevent data from being accessed by multiple threads, java provides two implementation methods: synchronization block and synchronization method. Once a piece of code is embedded in a synchronized keyword, it means that the data is placed in the monitoring area, JVM automatically locks the code in the background.
[3] Which part of JAVA's synchronization code is a monitor?
- We know that every JAVA Object (Object/class) is associated with a monitor. A better saying is that every Object (Object/class) has a monitor, and an Object can have its own critical section, to enable thread collaboration, JAVA provides wait (), yyall, and Y () to suspend threads and wake up another waiting thread, in addition, these methods have three different versions:
wait(long timeout, int nanos)wait(long timeout) notified by other threads or notified by timeout.notify(all)
These methods can only be called in one synchronization block or synchronous method because, if a method does not need to be mutually exclusive and does not need to be monitored or collaborate between threads, every thread can access this method freely without collaboration.
In the Monitor, how does one synchronize threads?
- The monitor and lock are used together in the Java Virtual Machine. The monitor monitors a synchronization code block and ensures that only one thread executes the synchronization code block at a time. Each monitor is associated with an object reference. The thread cannot execute the synchronization code before obtaining the lock.
Other related knowledge of the monitor. url:
- Http://wangym.iteye.com/blog/1265973
- Http://blog.csdn.net/lirx_tech/article/details/50913520
- Https://zhidao.baidu.com/question/2266270659590811188.html
- Implementation principle of Monitor and syncrhoized Based on Java synchronization mechanism: http://www.cnblogs.com/ygj0930/p/6561667.html