Monitors are an other mechanism of concurrent programming. It's a higher level mechanism than semaphores and also more powerful. A monitor is an instance of a class that can be used safely by several threads. All the methods of a monitor are executed with mutual exclusion. So at most one thread can execute a method of the monitor at the same time. This mutual exclusion policy makes easier to work with monitor and to develop the method content of the monitor.
Monitors have an other feature, the possibility to make a thread waiting for a condition. During the wait time, the thread temporarily gives up its exclusive access and must reacquire it after the condition has been met. You can also signal one or more threads that a condition has been met.
There is several advantages on using monitors instead of a lower-level mechanisms :
- All the synchronization code is centralized in one location and the users of this code don’t need to know how it’s implemented.
- The code doesn't depend on the number of processes, it works for as many processes as you want
- You don’t need to release something like a mutex, so you cannot forget to do it
Read full article from Java Concurrency - Part 5 : Monitors (Locks and Conditions) | @Blog("Baptiste Wicht")
No comments:
Post a Comment