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:
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With