java - Fairness setting in semaphore class -
i trying understand usefulness of fairness property in semaphore
class.
specifically quote javadoc mentions that:
generally, semaphores used control resource access should initialized fair, ensure no thread starved out accessing resource. when using semaphores other kinds of synchronization control, throughput advantages of non-fair ordering outweigh fairness considerations.
could provide example barging might desired here. cannot think past resource access use case. also, why default non-fair behavior?
lastly, there performance implications in using fairness behavior?
java's built-in concurrency constructs (synchronized
, wait()
, notify()
,...) not specify thread should freed when lock released. jvm implementation decide algorithm use.
fairness gives more control: when lock released, thread longest wait time given lock (fifo processing). without fairness (and bad algorithm) might have situation thread waiting lock because there continuous stream of other threads.
if semaphore set fair, there's small overhead because needs maintain queue of threads waiting lock. unless you're writing high throughput/high performance/many cores application, won't see difference though!
scenario fairness not needed
if have n identical worker threads, doesn't matter 1 gets task execute
scenario fairness needed
if have n tasks queues, don't want 1 queue waiting forever , never acquiring lock.
Comments
Post a Comment