[LANG-1750] Add support for FIFO fairness in TimedSemaphore and related test cases #1544
+257
−32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue: LANG-1790
I continued based on the changes and suggestions made in PR LANG-1656. The PR has been left unchanged since Oct 30 2025. I hereby acknowledge using that code and the reviewers suggestions as the starting point for my work.
According to the PR checklist I also acknowledge using the JetBrains AI in the writing of the PR description, not in the writing of the actual code. I have checked the description it wrote and I find it accurately describes the changes.
One major difference to the initial PR was having to remove the FIFO test provided in the original PR. When running the test multiple times (ten or more) it would occasionally fail. I have read up on the Semaphore and fairness in the
java.util.concurrent.Semaphoreclass and I believe that the order cannot be guaranteed even with fairness.I have also tried checking the tests in Open JDK for
java.util.concurrent.Semaphoreand I could not find a similar test in their repository.Summary
This PR enhances the
TimedSemaphoreclass by adding optional FIFO (fair ordering) semaphore support, allowing applications to enforce fairness guarantees when multiple threads compete for permits. This is particularly valuable in scenarios where strict ordering and prevention of thread starvation are critical requirements.Implementation
Key Changes:
Fairness Configuration: Added a
fairboolean field and correspondingBuilder.setFair(boolean)method to enable/disable fair semaphore ordering. Fair mode uses the underlyingjava.util.concurrent.Semaphorein fair mode, enforcing FIFO ordering.Backing Semaphore: Introduced a backing
Semaphoreinstance initialized with the fair flag, replacing direct permit management. This delegates fairness enforcement to the standard JDK semaphore implementation.Builder Pattern Enhancement: Extended the
Builderinner class with asetFair()method, providing a flexible fluent API for configuration alongside existing options (setPeriod(),setLimit(),setService(),setTimeUnit()).Acquisition Logic Updates:
fairTryAcquire()helper method that respects the fairness setting:semaphore.tryAcquire(0, TimeUnit.SECONDS)to honor the FIFO queuesemaphore.tryAcquire()for immediate acquisition attemptsacquire()andtryAcquire()methods to leverage the backing semaphorePeriod Management: Modified
endOfPeriod()to properly synchronize permit release through the backing semaphore, calculating the number of permits to release based on the current limit and available permits.Testing
The test suite validates:
ReflectionToStringBuilderConcurrencyTest.javaverify correct behavior under concurrent access patterns (note: some tests marked as@Disabledfor known edge cases)setLimit()properly adjusts semaphore stateBackward Compatibility
✅ Fully backward compatible. Existing code continues to work without modification:
Builder.setFair(true)Benefits