Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Thread-Specific Storage Pattern #3225

Open
6 tasks
iluwatar opened this issue Mar 30, 2025 · 0 comments
Open
6 tasks

Implement Thread-Specific Storage Pattern #3225

iluwatar opened this issue Mar 30, 2025 · 0 comments

Comments

@iluwatar
Copy link
Owner

Description

Thread-Specific Storage is a concurrency design pattern where each thread retains its own instance of a shared object, typically achieved using ThreadLocal<T> in Java. By isolating data to each thread, you avoid synchronization overhead and minimize concurrency issues. Common use cases include storing thread-specific contexts, caching stateful objects like DateFormat, or managing per-thread counters without risking data corruption or race conditions.

Key Elements

  • Isolation of State: Each thread has its own copy of the data, reducing shared mutable state.
  • ThreadLocal Utility: Java’s ThreadLocal<T> class provides a straightforward way to store data private to each thread.
  • Initialization & Cleanup: Properly initializing and cleaning up thread-local data is crucial to prevent memory leaks.
  • Practical Use Cases: Storing per-thread data such as locale-specific formatters, current transaction context, or local caches.

References

  1. Pattern-Oriented Software Architecture, Volume 2: Patterns for Concurrent and Networked Objects
  2. Java Design Patterns - Contribution Guidelines

Acceptance Criteria

  • A new module or package named thread-specific-storage (or similar) is added.
  • Demonstrates a clear use case of storing and retrieving thread-local data (e.g., per-thread state).
  • Includes a readme (or .md file) explaining how the pattern works, with code examples or diagrams.
  • Adheres to the repository’s coding style and naming conventions.
  • Includes unit tests verifying behavior in a multi-threaded scenario.
  • Passes all CI checks and meets the project’s contribution guidelines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

1 participant