Last active
April 18, 2023 05:04
-
-
Save saswata-dutta/883fdc5c88239d22c39d7faf865e63e8 to your computer and use it in GitHub Desktop.
Timed handling of requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.concurrent.CompletableFuture; | |
| import java.util.concurrent.ExecutionException; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.concurrent.TimeoutException; | |
| public class RequestHandler { | |
| private ExecutorService threadPool; | |
| public RequestHandler(int poolSize) { | |
| threadPool = Executors.newFixedThreadPool(poolSize); | |
| } | |
| public List<String> handleRequests(List<Request> requests, long timeoutMillis) throws InterruptedException, ExecutionException, TimeoutException { | |
| List<CompletableFuture<String>> futures = new ArrayList<>(); | |
| // Submit each request processing task to the thread pool and add the resulting CompletableFuture to the list | |
| for (Request request : requests) { | |
| futures.add(CompletableFuture.supplyAsync(() -> processRequest(request), threadPool)); | |
| } | |
| List<String> completedResponses = new ArrayList<>(); | |
| // Wait for completed futures within the timeout | |
| while (!futures.isEmpty()) { | |
| CompletableFuture<Object> completedFuture = CompletableFuture.anyOf(futures.toArray(new CompletableFuture[futures.size()])); | |
| try { | |
| String response = (String) completedFuture.get(timeoutMillis, TimeUnit.MILLISECONDS); | |
| completedResponses.add(response); | |
| } catch (TimeoutException e) { | |
| break; | |
| } | |
| futures.remove(completedFuture); | |
| } | |
| return completedResponses; | |
| } | |
| private String processRequest(Request request) { | |
| // Process the request here | |
| // ... | |
| return "response"; | |
| } | |
| public void stop() { | |
| threadPool.shutdown(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| import java.util.concurrent.CompletableFuture; | |
| import java.util.concurrent.ExecutionException; | |
| import java.util.stream.Collectors; | |
| public class RequestHandler { | |
| private ExecutorService threadPool; | |
| public RequestHandler(int poolSize) { | |
| threadPool = Executors.newFixedThreadPool(poolSize); | |
| } | |
| public List<String> handleRequests(List<Request> requests) throws InterruptedException, ExecutionException { | |
| List<CompletableFuture<String>> futures = requests.stream() | |
| .map(request -> CompletableFuture.supplyAsync(() -> processRequest(request), threadPool)) | |
| .collect(Collectors.toList()); | |
| CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); | |
| return allFutures.thenApply(v -> futures.stream() | |
| .map(CompletableFuture::join) | |
| .collect(Collectors.toList())) | |
| .get(); | |
| } | |
| private String processRequest(Request request) { | |
| // Process the request here | |
| // ... | |
| return "response"; | |
| } | |
| public void stop() { | |
| threadPool.shutdown(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.concurrent.*; | |
| public class RequestHandler { | |
| private ExecutorService threadPool; | |
| public RequestHandler(int poolSize) { | |
| threadPool = Executors.newFixedThreadPool(poolSize); | |
| } | |
| public String handleRequest(Request request, long timeoutMillis) throws TimeoutException, InterruptedException, ExecutionException { | |
| Future<String> future = threadPool.submit(() -> processRequest(request)); | |
| try { | |
| return future.get(timeoutMillis, TimeUnit.MILLISECONDS); | |
| } catch (TimeoutException e) { | |
| future.cancel(true); | |
| throw e; | |
| } | |
| } | |
| private String processRequest(Request request) { | |
| // Process the request here | |
| // ... | |
| return "response"; | |
| } | |
| public void stop() { | |
| threadPool.shutdown(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment