Iam_Sandeep

Untitled

Apr 15th, 2026
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.35 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.*;
  3. import java.lang.*;
  4. import java.io.*;
  5. import java.util.concurrent.*;
  6. import java.util.concurrent.atomic.AtomicInteger;
  7.  
  8. class Solution {
  9.     public sealed interface TaskOutcome permits TaskSuccess, TaskFailure, TaskTimeout { String taskId(); }
  10.     public record TaskSuccess(String taskId, Object result) implements TaskOutcome {}
  11.     public record TaskFailure(String taskId, Throwable error) implements TaskOutcome {}
  12.     public record TaskTimeout(String taskId) implements TaskOutcome {}
  13.     public record ExecutionSummary(int submitted, int success, int failed, int timeout, Map<String, TaskOutcome> snapshot) {}
  14.  
  15.     static class CustomBoundedMap<K, V> {
  16.         class Node { K k; V v; Node p, n; Node(K k, V v) { this.k = k; this.v = v; } }
  17.         private final int cap;
  18.         private final Map<K, Node> m = new HashMap<>();
  19.         private Node head, tail;
  20.  
  21.         CustomBoundedMap(int cap) { this.cap = cap; }
  22.  
  23.         public synchronized void put(K k, V v) {
  24.             if (m.containsKey(k)) { m.get(k).v = v; return; }
  25.             if (m.size() >= cap && head != null) {
  26.                 m.remove(head.k);
  27.                 head = head.n;
  28.                 if (head != null) head.p = null; else tail = null;
  29.             }
  30.             Node nn = new Node(k, v);
  31.             if (tail == null) head = tail = nn; else { tail.n = nn; nn.p = tail; tail = nn; }
  32.             m.put(k, nn);
  33.         }
  34.  
  35.         public synchronized Map<K, V> snapshot() {
  36.             Map<K, V> snap = new LinkedHashMap<>();
  37.             for (Node c = head; c != null; c = c.n) snap.put(c.k, c.v);
  38.             return snap;
  39.         }
  40.     }
  41.  
  42.     static class ProcessingEngine {
  43.         private final ExecutorService exec;
  44.         private final CustomBoundedMap<String, TaskOutcome> storage;
  45.  
  46.         public ProcessingEngine(int threads, int cap) {
  47.             this.exec = Executors.newFixedThreadPool(threads);
  48.             this.storage = new CustomBoundedMap<>(cap);
  49.         }
  50.  
  51.         public ExecutionSummary processTasks(Map<String, Callable<Object>> tasks, long timeout, TimeUnit unit) {
  52.             var s = new AtomicInteger(); var f = new AtomicInteger(); var t = new AtomicInteger();
  53.             var futures = tasks.entrySet().stream().map(e -> CompletableFuture.supplyAsync(() -> {
  54.                 try { return e.getValue().call(); } catch (Exception ex) { throw new CompletionException(ex); }
  55.             }, exec).orTimeout(timeout, unit).handle((res, ex) -> {
  56.                 String id = e.getKey();
  57.                 if (ex == null) { s.incrementAndGet(); storage.put(id, new TaskSuccess(id, res)); }
  58.                 else if (ex.getCause() instanceof TimeoutException) { t.incrementAndGet(); storage.put(id, new TaskTimeout(id)); }
  59.                 else { f.incrementAndGet(); storage.put(id, new TaskFailure(id, ex.getCause())); }
  60.                 return null;
  61.             })).toArray(CompletableFuture[]::new);
  62.  
  63.             CompletableFuture.allOf(futures).join();
  64.             return new ExecutionSummary(tasks.size(), s.get(), f.get(), t.get(), storage.snapshot());
  65.         }
  66.  
  67.         public void shutdown() { exec.shutdown(); }
  68.     }
  69.  
  70.     public void solve() {
  71.         ProcessingEngine engine = new ProcessingEngine(4, 4);
  72.         Map<String, Callable<Object>> tasks = new LinkedHashMap<>();
  73.        
  74.         tasks.put("task-1", () -> "Completed Fast");
  75.         tasks.put("task-2", () -> { throw new RuntimeException("Simulated Failure"); });
  76.         tasks.put("task-3", () -> { Thread.sleep(2000); return "Will Timeout"; });
  77.         tasks.put("task-4", () -> "Completed Fast 2");
  78.         tasks.put("task-5", () -> "Completed Fast 3");
  79.  
  80.         ExecutionSummary summary = engine.processTasks(tasks, 500, TimeUnit.MILLISECONDS);
  81.        
  82.         System.out.println("Submitted: " + summary.submitted());
  83.         System.out.println("Success:   " + summary.success());
  84.         System.out.println("Failed:    " + summary.failed());
  85.         System.out.println("Timeout:   " + summary.timeout());
  86.        
  87.         summary.snapshot().forEach((key, outcome) ->
  88.             System.out.println(key + " -> " + outcome.getClass().getSimpleName())
  89.         );
  90.  
  91.         engine.shutdown();
  92.     }
  93. }
  94.  
  95. public class Main {
  96.     public static void main(String[] args) {
  97.         Solution solution = new Solution();
  98.         solution.solve();
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment