Semaphore and Mutex

Discussions

General J2EE: Semaphore and Mutex

  1. Semaphore and Mutex (2 messages)

    I would like to have an explanation on Semaphore and Mutex on very basic terms.

    What I know abt Semaphore (wrt Java) is that it can be visualized as a Java Class with an int as attribute... and 2 methods... up() and down().

    up() would increment the value of the int and down() would decrement the value if it is greater than 0.

    Mutex... Mutual Exclusion... dont understand much on this... in Mutex... an array of Semaphore Objects is used in Multithread environment.

    Threaded Messages (2)

  2. Semaphore and Mutex[ Go to top ]

    I suppose you know all of this is used for concurrent and multi-threading programing...
    These semaphores are used to "inform" to the applications that another application or thread is accesing to data or perform some operation that must be executed without any interruption (this is called a critical region).
    I guess you will find this useful:

    http://www.fact-index.com/m/mu/mutual_exclusion.html

    Semaphores are the java implementation for mutual exclusion, if a thread try to access to the critical region, before must check the semaphore, if the value of the semaphore is 0, the thread must wait until another thread leaves this critical region, when this occurs the leaving thread increments the value of the semaphore and make a call to the notify() method that "awakes" a waiting thread, if the value of semaphore is 1 this thread can access to the critical region and must decrease semaphore's value until leaving.

    Hope this will help.
  3. Semaphore and Mutex[ Go to top ]

    A distinction needs to be made here. A semaphore is used to manage a particular resource, for example a JDBC connection pool. It contains a set of permits. When the permits have all been issued, the call to acquire this semaphore will block.

    A mutex on the other hand simply allows single-threaded execution of a critical region. Only one thread may acquire the mutex at a time.

    So a semaphore with permit count = 1 acts like a mutex.