-
Notifications
You must be signed in to change notification settings - Fork 243
Task to check #236
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
base: main
Are you sure you want to change the base?
Task to check #236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,46 @@ | ||
| package mate.academy; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.Executor; | ||
|
|
||
| public class AsyncRequestProcessor { | ||
|
|
||
| private final Executor executor; | ||
|
|
||
| private final Map<String, UserData> cache = new ConcurrentHashMap<>(); | ||
| private final Map<String, CompletableFuture<UserData>> inFlight = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| public AsyncRequestProcessor(Executor executor) { | ||
| this.executor = executor; | ||
| } | ||
|
|
||
| public CompletableFuture<UserData> processRequest(String userId) { | ||
| return null; | ||
| UserData cached = cache.get(userId); | ||
| if (cached != null) { | ||
| return CompletableFuture.completedFuture(cached); | ||
| } | ||
|
|
||
| return inFlight.computeIfAbsent(userId, id -> | ||
| CompletableFuture.supplyAsync(() -> { | ||
| simulateDelay(); | ||
| return new UserData(id, "Details for " + id); | ||
| }, executor).whenComplete((result, error) -> { | ||
| inFlight.remove(id); | ||
| if (error == null) { | ||
| cache.put(id, result); | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| private void simulateDelay() { | ||
| try { | ||
| Thread.sleep(1000); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,21 @@ | ||
| package mate.academy; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| public class Main { | ||
| 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 | ||
| AsyncRequestProcessor asyncRequestProcessor = new AsyncRequestProcessor(executor); | ||
| ExecutorService executor = Executors.newFixedThreadPool(4); | ||
| AsyncRequestProcessor processor = new AsyncRequestProcessor(executor); | ||
|
|
||
| // Simulating multiple concurrent requests | ||
| String[] userIds = {"user1", "user2", "user3", "user1"}; // Note: "user1" is repeated | ||
| CompletableFuture<?>[] futures = new CompletableFuture[userIds.length]; | ||
| String[] userIds = {"user1", "user2", "user3", "user1"}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The input array matches the requirement ( |
||
|
|
||
| for (int i = 0; i < userIds.length; i++) { | ||
| String userId = userIds[i]; | ||
| futures[i] = asyncRequestProcessor.processRequest(userId) | ||
| .thenAccept(userData -> System.out.println("Processed: " + userData)); | ||
| for (String userId : userIds) { | ||
| processor.processRequest(userId) | ||
| .thenAccept(result -> | ||
| System.out.println("Processed: " + result)); | ||
| } | ||
|
|
||
| // Wait for all futures to complete | ||
| CompletableFuture.allOf(futures).join(); | ||
| executor.shutdown(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| package mate.academy; | ||
|
|
||
| public record UserData(String userId, String details) { | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lambda constructs the
UserDatacorrectly ("Details for " + id) which matches checklist item #16 — good. However, after theUserDatais produced you must store the result into the requiredMap<String, UserData> cache(checklist item #13). Consider attaching awhenComplete/thenApplyhandler on the in-flight future to put the finishedUserDataintocacheand remove the entry from the in-flight map.