Skip to content

Add thread-safe queue implementation#7388

Open
Senrian wants to merge 2 commits intoTheAlgorithms:masterfrom
Senrian:add/thread-safe-queue
Open

Add thread-safe queue implementation#7388
Senrian wants to merge 2 commits intoTheAlgorithms:masterfrom
Senrian:add/thread-safe-queue

Conversation

@Senrian
Copy link
Copy Markdown
Contributor

@Senrian Senrian commented Apr 19, 2026

Description

Adds a simple thread-safe queue implementation (ThreadSafeQueue) to the data structures directory.

This implementation uses the synchronized keyword on methods to ensure thread safety, as requested in issue #7387.

Features:

  • Thread-safe operations using synchronized keyword
  • Node-based linked list internal implementation
  • Basic operations: enqueue, dequeue, peek, isEmpty, size
  • Implements Iterable<T> for enhanced usability
  • Generic type parameter for flexibility

Implementation details:

  • Uses internal Node<T> class for linked list nodes
  • All public methods are synchronized for thread safety
  • Throws NoSuchElementException when dequeuing/peeking from empty queue
  • Throws IllegalArgumentException when enqueueing null data

Fixes #7387


Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • 🚨 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📝 Documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Senrian added 2 commits April 1, 2026 08:34
Issue TheAlgorithms#7356: Add null check for the search value to prevent potential NullPointerException.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 56 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.37%. Comparing base (cc75b5e) to head (91b762f).
⚠️ Report is 23 commits behind head on master.

Files with missing lines Patch % Lines
...orithms/datastructures/queues/ThreadSafeQueue.java 0.00% 54 Missing ⚠️
.../java/com/thealgorithms/searches/BinarySearch.java 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7388      +/-   ##
============================================
- Coverage     79.45%   79.37%   -0.09%     
- Complexity     7083     7179      +96     
============================================
  Files           790      799       +9     
  Lines         23164    23521     +357     
  Branches       4556     4623      +67     
============================================
+ Hits          18405    18669     +264     
- Misses         4022     4108      +86     
- Partials        737      744       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Shoyeb45
Copy link
Copy Markdown
Contributor

Nice work on the implementation!

I was planning to start working on this after opening the issue, but great to see it already in progress.

If you're okay with it, I can contribute by adding test cases to strengthen the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Add a simple thread-safe queue implementation.

3 participants