Initial commit.#244
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Good job setting up the asynchronous execution flow with CompletableFuture and an ExecutorService. However, this requires changes because the two central requirements of the task have not been implemented: caching and a simulated processing delay.
In your AsyncRequestProcessor class, you need to:
- Add Caching Logic: Introduce a
ConcurrentHashMapto storeUserDataresults. Before creating a newUserDataobject, yourprocessRequestmethod must first check this map. If the data for auserIdexists, it should be returned from the cache immediately. - Simulate a Delay: Inside the
CompletableFuture.supplyAsyncblock, add a delay usingThread.sleep()to simulate a long-running operation. This is crucial for demonstrating the benefit of the caching mechanism.
Once these two features are implemented, your solution will meet all the core requirements. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| UserData userData = new UserData(userId, "Details for " + userId); | ||
| return CompletableFuture.supplyAsync(() -> userData, executor); |
There was a problem hiding this comment.
This implementation is missing the core caching logic and the simulated processing delay. According to the requirements, you should:
- Add a
ConcurrentHashMapcache as a field in this class. - Before creating a new
UserDataobject, check if the data for the givenuserIdis already in the cache. If it is, return the cached value directly (wrapped in a completedCompletableFuture). - Add a simulated delay inside the
supplyAsynctask to mimic a real-world, long-running operation. - After successfully creating a new
UserDataobject, store it in the cache before returning it.
| public static void main(String[] args) { | ||
| // Feel free to play with AsyncRequestProcessor in this main method if you want | ||
| ExecutorService executor = null; // Provide implementation that fits your needs | ||
| public static void main(String[] args) throws InterruptedException { |
There was a problem hiding this comment.
While this method correctly sets up and runs the asynchronous tasks, the overall application behavior will not meet the requirements. The AsyncRequestProcessor class that is used here is missing the mandatory caching implementation. As a result, repeated requests for the same userId will be fully processed every time instead of being served from the cache.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on this iteration! You have successfully addressed all the previous feedback and met all the requirements of the task. The caching logic with ConcurrentHashMap is correctly implemented, and the use of CompletableFuture for asynchronous processing is spot on.
I have left one minor suggestion in src/main/java/mate/academy/Main.java regarding an unnecessary Thread.sleep(). This is just a small point for improvement and doesn't block the approval.
Your solution is now complete and correct, so I am approving your submission. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| ExecutorService executor = Executors.newFixedThreadPool(2); | ||
| executor.execute(() -> System.out.println("Executor started")); | ||
|
|
||
| Thread.sleep(2000); |
There was a problem hiding this comment.
This Thread.sleep(2000) call introduces an unnecessary 2-second delay at the start of the program. The ExecutorService is ready for use immediately after creation, so this wait isn't needed. You can safely remove this line.
No description provided.