M
M
MaxLich2017-05-03 18:29:30
Java
MaxLich, 2017-05-03 18:29:30

What is the difference between semaphore, mutex and monitor? Or is it one and the same?

In different sources I find different definitions of these terms. Somewhere it was written that a mutex is essentially an object associated with each object in Java, and which has two states: busy and free. And about the monitor in the same article it was said that a special mechanism (block of code) that is responsible for managing access to some resource (object). Like, the monitor provides access to the resource of only one thread. And all other threads have to wait for this resource to be released. That is, this mechanism is responsible for capturing a resource by a thread, working with it in exclusive mode, and also for the subsequent release of this resource by the thread. In some articles it is written that the opposite is true (a monitor is an object associated with another object and has a state of "busy" or "free", and a mutex is just a mechanism, resource access manager). Eckel (in translation), for example, says this:

To solve the problem of thread contention, virtually all multithreading 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 the piece of code at a time. Since such a locking proposal has the effect of mutual exclusion, this mechanism is often referred to as a mutex (MUTual Exclusion).
Java has built-in support for conflict prevention in the form of the synchronized keyword. When a thread wishes to execute a piece of code guarded by the synchronized word, it checks to see if the semaphore is available, accesses the semaphore, executes the code, and releases the semaphore.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
programmerjava, 2017-05-03
@programmerjava

Well, Wikipedia says in the second paragraph that a mutex is a kind of semaphore.
https://ru.wikipedia.org/wiki/%D0%9C%D1%8C%D1%8E%D...

M
MaxLich, 2017-05-04
@MaxLich

And in Java, how is it all implemented? As I understand it, there is a separate class for the semaphore. A mutex and a monitor in the form of what is implemented there? what does it look like there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question