Skip to content

Conversation

dyrpsf
Copy link

@dyrpsf dyrpsf commented Oct 2, 2025

What does this PR do?

This PR implements the Timeout pattern for the java-design-patterns repository. The Timeout pattern prevents operations from blocking indefinitely by setting a maximum time limit for their completion. If an operation doesn't complete within the specified timeout period, it is cancelled and control is returned to the caller with an error.

Fixes #2845

Changes Made

  • Implemented TimeoutHandler class for executing operations with timeout constraints using ExecutorService
  • Created TimeoutException custom exception for handling timeout errors
  • Added App.java with three practical examples demonstrating the pattern:
    • Fast service call (completes within timeout)
    • Slow service call (exceeds timeout)
    • Database query with timeout
  • Included comprehensive unit tests (TimeoutHandlerTest.java) with 4 test cases:
    • Successful execution within timeout
    • Timeout exception when operation exceeds limit
    • Immediate return for fast operations
    • Exception handling when task throws errors
  • Created detailed README.md with:
    • Pattern intent and explanation
    • Real-world examples
    • Programmatic code examples
    • Class diagram
    • Applicability scenarios
    • Known uses in industry
    • Consequences (benefits and trade-offs)
    • Related patterns
  • Added UML class diagram (timeout.urm.png and .puml source)
  • Added pom.xml with required dependencies

Testing

All unit tests are included and functional. The implementation has been tested with:

  • Operations that complete within timeout limits
  • Operations that exceed timeout limits
  • Immediate return scenarios
  • Exception handling during task execution

Additional Notes

This is my first contribution to the java-design-patterns repository for Hacktoberfest 2024. I've followed the existing pattern structure and conventions used in other patterns in the repository.

Copy link

github-actions bot commented Oct 2, 2025

Analyzing changes in this PR...

This might take a few minutes, please wait

📥 Commits

Analyzing changes from base (ede37bd) to latest commit (ea2f22d):

📁 Files being considered (8)

➕ timeout/README.md (1 hunk)
➕ timeout/etc/timeout.urm.png (0 hunks)
➕ timeout/etc/timeout.urm.puml (1 hunk)
➕ timeout/pom.xml (1 hunk)
➕ timeout/src/main/java/com/iluwatar/timeout/App.java (1 hunk)
➕ timeout/src/main/java/com/iluwatar/timeout/TimeoutException.java (1 hunk)
➕ timeout/src/main/java/com/iluwatar/timeout/TimeoutHandler.java (1 hunk)
➕ timeout/src/test/java/com/iluwatar/timeout/TimeoutHandlerTest.java (1 hunk)


autogenerated by presubmit.ai

Copy link

sonarqubecloud bot commented Oct 2, 2025

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.

Timeout pattern
1 participant