Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semaphores: Where do I learn about basic concepts such as permits, fairness, barging, etc

The Semaphore class overview in developer.android.com looks pretty good - for those who are already familiar with the concepts and terminology.

I am familiar with some of the acronyms and other jargon there (e.g. FIFO, lock, etc.) but others such as permits, fairness and barging are new to me.

Can you recommend a good online source for explaining these concepts? (I can probably figure out what permits and fairness are but barging is an unknown at this point).

EDIT: After receiving the two answers below, I realized that I need a refresh on semaphores (to re-acquire() terminology). I found the following resources to be useful:

  1. Semaphore_(programming)
  2. Introduction to Semaphores by Dr. Richard S. Hall
like image 968
srf Avatar asked May 01 '11 00:05

srf


People also ask

What is semaphore permit?

A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource.

Why are semaphores fair?

Generally, semaphores used to control resource access should be initialized as fair, to ensure that no thread is starved out from accessing a resource. When using semaphores for other kinds of synchronization control, the throughput advantages of non-fair ordering often outweigh fairness considerations.

What are important methods provided by semaphore in maintaining concurrency?

Method SummaryAcquires the given number of permits from this semaphore, blocking until all are available. Returns the current number of permits available in this semaphore. Acquires and returns all permits that are immediately available. Returns a collection containing threads that may be waiting to acquire.

What is the semaphore in Java?

In Java, Semaphore is used to attain Process Synchronization. Semaphore in Java is a thread synchronization construct that avoids missed signals between threads by sending signals to the threads and protecting critical sections. With the use of counters, Semaphore manages access to the shared resources.


2 Answers

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html

This is an excerpt from what is considered one of the seminal works in java concurrency you should check it out. http://my.safaribooksonline.com/book/programming/java/0321349601/explicit-locks/287

like image 82
nsfyn55 Avatar answered Oct 24 '22 02:10

nsfyn55


Hadn't come across these myself, but thought I'd research and summarise my findings as it's better to in-line answers than link externally (although, yes, the OP is after recommending reading):

permits are the number of concurrent accesses allowed to the semaphore-protected code. Although often semaphores are simple Mutex's, it is sometimes desirable to have more than one thread touching code. This is similar to phoning a call-centre, where there's one phone number connected to 8 lines/operators.

fairness is when a semaphore is made available to requesters in strict order of who requested first. Staying with the call-centre analogy, this means the on-hold queue is a strict FIFO.

barging is essentially an out-of-band request, that puts a thread to the top of the queue for a semaphore. The analogy is where preferred customers (or internal calls) go to the top of a call-centre queue, rather than waiting their turn.

If neither fairness nor barging are specified, then it's within spec to grant access to the most recent request, depending on timing of context switches. The 'phone analogy is a call to a company switchboard/reception, where even if calls are on hold waiting for answer, you may get lucky and ring between one call ending and another call being taken off-hold.

Let me know through comments if I've got this wrong, and I'll fix / cw my answer.

like image 22
Phil Lello Avatar answered Oct 24 '22 02:10

Phil Lello