Iam_Sandeep

Untitled

Apr 15th, 2026
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4. class Solution {
  5.  
  6.     // Java 17 record
  7.     record TaskResult(String taskId, String status, String result) {}
  8.  
  9.     // Custom thread-safe map with eviction + order
  10.     static class CustomMap<K, V> {
  11.         private final int capacity;
  12.         private final LinkedHashMap<K, V> map;
  13.  
  14.         public CustomMap(int capacity) {
  15.             this.capacity = capacity;
  16.             this.map = new LinkedHashMap<>();
  17.         }
  18.  
  19.         public synchronized void put(K key, V value) {
  20.             if (map.size() >= capacity) {
  21.                 K oldestKey = map.keySet().iterator().next();
  22.                 map.remove(oldestKey); // evict oldest
  23.             }
  24.             map.put(key, value);
  25.         }
  26.  
  27.         public synchronized List<V> snapshot() {
  28.             return new ArrayList<>(map.values());
  29.         }
  30.     }
  31.  
  32.     public void solve() {
  33.  
  34.         int capacity = 5;
  35.         CustomMap<String, TaskResult> store = new CustomMap<>(capacity);
  36.  
  37.         ExecutorService executor = Executors.newFixedThreadPool(5);
  38.  
  39.         List<CompletableFuture<Void>> futures = new ArrayList<>();
  40.  
  41.         for (int i = 1; i <= 10; i++) {
  42.             String taskId = "Task-" + i;
  43.  
  44.             CompletableFuture<Void> future = CompletableFuture
  45.                 .supplyAsync(() -> {
  46.                     try {
  47.                         // simulate work
  48.                         Thread.sleep(new Random().nextInt(2000));
  49.                         if (new Random().nextInt(5) == 0) {
  50.                             throw new RuntimeException("Error");
  51.                         }
  52.                         return "Result-" + taskId;
  53.                     } catch (Exception e) {
  54.                         throw new RuntimeException(e);
  55.                     }
  56.                 }, executor)
  57.  
  58.                 // timeout handling
  59.                 .orTimeout(1, TimeUnit.SECONDS)
  60.  
  61.                 .handle((res, ex) -> {
  62.                     if (ex != null) {
  63.                         if (ex instanceof TimeoutException) {
  64.                             store.put(taskId, new TaskResult(taskId, "TIMEOUT", null));
  65.                         } else {
  66.                             store.put(taskId, new TaskResult(taskId, "FAILED", null));
  67.                         }
  68.                     } else {
  69.                         store.put(taskId, new TaskResult(taskId, "SUCCESS", res));
  70.                     }
  71.                     return null;
  72.                 });
  73.  
  74.             futures.add(future);
  75.         }
  76.  
  77.         // wait for all tasks
  78.         CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
  79.  
  80.         executor.shutdown();
  81.  
  82.         // summary
  83.         List<TaskResult> results = store.snapshot();
  84.  
  85.         long success = results.stream().filter(r -> r.status.equals("SUCCESS")).count();
  86.         long failed = results.stream().filter(r -> r.status.equals("FAILED")).count();
  87.         long timeout = results.stream().filter(r -> r.status.equals("TIMEOUT")).count();
  88.  
  89.         System.out.println("Summary:");
  90.         System.out.println("SUCCESS: " + success);
  91.         System.out.println("FAILED: " + failed);
  92.         System.out.println("TIMEOUT: " + timeout);
  93.  
  94.         System.out.println("\nOrdered Snapshot:");
  95.         for (TaskResult r : results) {
  96.             System.out.println(r);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment