Why is the Java Semaphore fairness the constructor argument and not the purchase () / tryAcquire () calls?

I see absolutely no reason why this is not a per-call option. The semaphore can be knocked down in different code paths (progress check master / queue workers / ...) with different fairness requirements. Perhaps in one case we want to check progress fairly, while in another we want the same workers to work if the work for workers is only enough to optimize L1 cache hits, etc.)

+3


source to share


1 answer


tryAcquire

is not fair even if you set it fair

to true in the constructor.

Even if this semaphore was configured to use a fair ordering policy, the call tryAcquire()

will be allowed immediately if one, whether other threads are currently available. This "barging" behavior can be useful under certain circumstances, although it violates fairness. If you want to be fair, then use tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS)

which is nearly equivalent (it also detects interruptions).

- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html#tryAcquire ()




I think this behavior would be enough to enable the scenario you described (checking progress and grabbing work).

0


source







All Articles