java – What are Monitor, Mutex, and Semaphore? Are they the same thing or different things?


It's just that it's written differently everywhere. And I just can't understand what each of these concepts actually means. For example, the same Eckel has the following lines about this:

To solve the problem of thread contention, virtually all multithreaded schemes synchronize access to shared resources. This means that only one thread can access a shared resource at a time. Most often, this is done by placing a piece of code in a blocking section so that only one thread can traverse this piece of code at a time. Because this locking clause has the effect of mutual exclusion, this mechanism is often referred to as a MUTual Exclusion.

Java has built-in support for conflict avoidance in the form of the synchronized keyword. When a thread wants to execute a piece of code protected by the word synchronized, it checks to see if the semaphore is available, accesses the semaphore, executes the code, and releases the semaphore.

I myself read in one article (and so far I adhere to this understanding) that a mutex is an object that is associated with every object in Java, and which can take two states: busy and free. And about the monitor in the same articles it was written that this is some special mechanism (in fact, a piece of code) that, using a mutex, regulates thread access to a certain block of code (that is, it is responsible for capturing a thread of a resource, and allows only one given the thread (which captured the mutex) to follow this block of code protected by the monitor; accordingly, the monitor does not allow other threads to occupy this resource and this block of code; and when the thread exits this block of code, the monitor releases the mutex and allows other threads to enter this block of code). Is this a correct understanding?

And what exactly do these concepts mean in Java?


In general, you can think of a mutex as a special case of a semaphore. The semaphore works simply – it has a certain initial number – a counter. Each time a thread "grabs" a semaphore, this number is decremented by one. If it is equal to zero or less than zero, the semaphore is locked (that is, roughly speaking, the code stops at the moment of seizing the semaphore). Each time the semaphore is released, this counter is incremented by one. And if after that it turns out to be greater than zero, then a wake-up signal will be sent to some arbitrary thread that "hangs on the semaphore capture" and it will be able to continue working (of course, by decreasing the counter value again).

The most obvious example of a semaphore is download managers – we add a lot of files to download, but we limit the number of simultaneous downloads.

And a mutex is just a semaphore that is initialized with a seed of 1. Therefore, only one thread can capture it. But as always, there are exceptions. There are so-called read-write mutexes (RWLock). These mutexes can be captured for reading and writing. And if many threads can capture for reading, then only one for writing (yes, and at this moment no one else can read).

that a mutex is an object that is associated with every object in Java, and which can take two states: busy and free.

this is almost close to the truth. Yes, in java the Object class has a mutex field. And all heirs (read all other classes) also receive a personal mutex. if a method is marked with the word syncronized, then it just wraps its call in this mutex. That is, if you call syncronized methods on the same object from different threads, the mutex will prevent them from being executed in parallel. But for different objects (even of the same type) this no longer works.

Scroll to Top