Skip to content

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

@Shoyeb45

Description

@Shoyeb45

What would you like to Propose?

I would like to add a simple thread-safe queue implementation using Java synchronization.

The goal is to demonstrate how basic data structures behave in a multithreaded environment and how mutual exclusion can be achieved using synchronized.

Issue details

Proposed Implementation

  • Data Structure: Thread-safe Queue
  • Approach:
    • Use a custom linked list (Node-based implementation)
    • Synchronize critical methods (enqueue, dequeue, etc.)
    • Keep the implementation simple and educational

Additional Information

Motivation

The repository focuses on educational implementations of data structures and algorithms. However, most implementations assume a single-threaded environment.

Introducing a basic concurrent data structure would:

  • Help learners understand synchronization and thread safety
  • Demonstrate issues like race conditions and how to prevent them
  • Serve as a foundation for more advanced concurrent structures

Scope

  • Add a ThreadSafeQueue implementation
  • Include detailed documentation explaining:
    • synchronization
    • critical sections
    • limitations (coarse-grained locking)
  • Add a simple multithreaded usage example
  • The implementation can be placed inside datastructures/threadsafe/ThreadSafeQueue.java(suggestion if there is any better place)
  • or Inside queueus/ThreadSafeQueue.java

Future Work

If this is accepted, similar implementations can be added for:

  • Stack
  • Deque
  • Blocking Queue (using wait/notify)
  • HashMap
  • Sets, etc..

Contribution

I would like to work on this implementation. Please let me know if this approach looks good, and I can proceed with the implementation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions