feat(datastructures): add thread-safe bounded queue implementation#7428
Open
MD-Mushfiqur123 wants to merge 5 commits into
Open
feat(datastructures): add thread-safe bounded queue implementation#7428MD-Mushfiqur123 wants to merge 5 commits into
MD-Mushfiqur123 wants to merge 5 commits into
Conversation
Implements a thread-safe blocking queue using ReentrantLock and Condition variables for producer-consumer synchronization. ### What This Adds **ThreadSafeQueue.java** - Thread-safe bounded queue: - `enqueue()` - Blocking add to tail, waits when queue is full - `dequeue()` - Blocking remove from head, waits when queue is empty - `offer()` - Non-blocking add, returns false when full - `poll()` - Non-blocking remove, returns null when empty - `size()`, `isEmpty()`, `isFull()`, `capacity()` - State queries - Uses circular buffer for O(1) enqueue/dequeue operations - Supports multiple concurrent producers and consumers **ThreadSafeQueueTest.java** - Comprehensive test suite: - Basic enqueue/dequeue operations - Offer/poll non-blocking behavior - Null rejection validation - Invalid capacity rejection - Circular buffer wrap-around - Multiple producers single consumer concurrency - Single producer multiple consumers concurrency - Blocking behavior verification - Stress test with 8 concurrent threads ### Algorithm Uses a circular buffer with ReentrantLock and two Condition variables: - `notFull` - signaled when space becomes available - `notEmpty` - signaled when items are added - Producers await notFull when buffer is full - Consumers await notEmpty when buffer is empty - Signal opposite condition after each operation Time: O(1) enqueue/dequeue | Space: O(n) bounded buffer ### Reference https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem
- testOfferPoll: Changed capacity from 3 to 2 so third offer correctly fails - testMultipleProducersSingleConsumer: Removed startLatch, use dedicated consumer thread with synchronized results list for thread safety
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7428 +/- ##
============================================
+ Coverage 79.63% 79.68% +0.05%
- Complexity 7241 7259 +18
============================================
Files 800 801 +1
Lines 23608 23672 +64
Branches 4646 4653 +7
============================================
+ Hits 18800 18864 +64
Misses 4055 4055
Partials 753 753 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Checkstyle flagged UnusedImports violation for org.junit.jupiter.api.Assertions.assertArrayEquals which was not used in any test method.
…_NOT_SIGNALALL SpotBugs flags all four Condition.signal() calls in ThreadSafeQueue as Medium severity bugs (MDM_SIGNAL_NOT_SIGNALALL). In a multi-producer/multi-consumer scenario, signal() wakes only one waiting thread, which can cause deadlock when multiple producers or consumers are blocked on the same condition variable. Using signalAll() ensures all waiting threads are notified and can re-check their loop condition, preventing the lost-wakeup problem that occurs when a single signal wakes a thread that cannot make progress. This change affects enqueue(), dequeue(), offer(), and poll() methods where notEmpty.signal() and notFull.signal() are replaced with notEmpty.signalAll() and notFull.signalAll() respectively.
…oManyStaticImports PMD flags TooManyStaticImports when more than 4 static imports are present. The test file had 5 static imports from org.junit.jupiter.api.Assertions (equals, assertFalse, assertNull, assertThrows, assertTrue) which exceeded the default threshold. Replaced with regular import and Assertions. prefix to eliminate the PMD violation while maintaining readability.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change:
Checklist:
mvn clean verify.Thread-Safe Queue (Producer-Consumer)
A blocking queue that supports multiple producers and consumers using ReentrantLock and Condition variables.
What This Adds
ThreadSafeQueue.java - Thread-safe bounded queue:
enqueue()- Blocking add to tail, waits when queue is fulldequeue()- Blocking remove from head, waits when queue is emptyoffer()- Non-blocking add, returns false when fullpoll()- Non-blocking remove, returns null when emptysize(),isEmpty(),isFull(),capacity()- State queriesThreadSafeQueueTest.java - Comprehensive test suite (15 tests):
Algorithm
Uses a circular buffer with ReentrantLock and two Condition variables:
notFull- signaled when space becomes availablenotEmpty- signaled when items are addedTime: O(1) enqueue/dequeue | Space: O(n) bounded buffer
Reference
https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem