Iam_Sandeep

Untitled

Apr 15th, 2026
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3. import java.util.concurrent.atomic.AtomicInteger;
  4. import java.util.concurrent.locks.ReentrantLock;
  5.  
  6. public class ProcessingEngine {
  7.  
  8.     public sealed interface TaskOutcome permits TaskSuccess, TaskFailure, TaskTimeout {
  9.         String taskId();
  10.     }
  11.     public record TaskSuccess(String taskId, Object result) implements TaskOutcome {}
  12.     public record TaskFailure(String taskId, Throwable error) implements TaskOutcome {}
  13.     public record TaskTimeout(String taskId) implements TaskOutcome {}
  14.  
  15.     public record ExecutionSummary(
  16.             int submitted, int success, int failed, int timeout, Map<String, TaskOutcome> snapshot
  17.     ) {}
  18.  
  19.     static class BoundedOrderedConcurrentMap<K, V> {
  20.         private final int capacity;
  21.         private final ReentrantLock lock = new ReentrantLock();
  22.         private final Map<K, Node<K, V>> map = new HashMap<>();
  23.         private Node<K, V> head, tail;
  24.  
  25.         private static class Node<K, V> {
  26.             K key; V value; Node<K, V> prev, next;
  27.             Node(K key, V value) { this.key = key; this.value = value; }
  28.         }
  29.  
  30.         public BoundedOrderedConcurrentMap(int capacity) { this.capacity = capacity; }
  31.  
  32.         public void put(K key, V value) {
  33.             lock.lock();
  34.             try {
  35.                 if (map.containsKey(key)) {
  36.                     map.get(key).value = value;
  37.                     return;
  38.                 }
  39.                 if (map.size() >= capacity && head != null) {
  40.                     map.remove(head.key);
  41.                     removeNode(head);
  42.                 }
  43.                 Node<K, V> newNode = new Node<>(key, value);
  44.                 addLast(newNode);
  45.                 map.put(key, newNode);
  46.             } finally { lock.unlock(); }
  47.         }
  48.  
  49.         private void removeNode(Node<K, V> node) {
  50.             if (node.prev != null) node.prev.next = node.next; else head = node.next;
  51.             if (node.next != null) node.next.prev = node.prev; else tail = node.prev;
  52.         }
  53.  
  54.         private void addLast(Node<K, V> node) {
  55.             if (tail == null) head = tail = node;
  56.             else { tail.next = node; node.prev = tail; tail = node; }
  57.         }
  58.  
  59.         public Map<K, V> getSnapshot() {
  60.             lock.lock();
  61.             try {
  62.                 Map<K, V> snapshot = new LinkedHashMap<>();
  63.                 for (Node<K, V> curr = head; curr != null; curr = curr.next) {
  64.                     snapshot.put(curr.key, curr.value);
  65.                 }
  66.                 return snapshot;
  67.             } finally { lock.unlock(); }
  68.         }
  69.     }
  70.  
  71.     private final ExecutorService executor;
  72.     private final BoundedOrderedConcurrentMap<String, TaskOutcome> storage;
  73.  
  74.     public ProcessingEngine(int poolThreads, int maxCapacity) {
  75.         this.executor = Executors.newFixedThreadPool(poolThreads);
  76.         this.storage = new BoundedOrderedConcurrentMap<>(maxCapacity);
  77.     }
  78.  
  79.     public ExecutionSummary processTasks(Map<String, Callable<Object>> tasks, long timeout, TimeUnit unit) {
  80.         AtomicInteger success = new AtomicInteger(0), failed = new AtomicInteger(0), timedOut = new AtomicInteger(0);
  81.         List<CompletableFuture<Void>> futures = new ArrayList<>();
  82.  
  83.         for (var entry : tasks.entrySet()) {
  84.             String taskId = entry.getKey();
  85.             futures.add(CompletableFuture.supplyAsync(() -> {
  86.                 try { return entry.getValue().call(); }
  87.                 catch (Exception e) { throw new CompletionException(e); }
  88.             }, executor)
  89.             .orTimeout(timeout, unit)
  90.             .handle((res, ex) -> {
  91.                 TaskOutcome outcome;
  92.                 if (ex != null) {
  93.                     Throwable cause = ex instanceof CompletionException ? ex.getCause() : ex;
  94.                     if (cause instanceof TimeoutException) {
  95.                         outcome = new TaskTimeout(taskId);
  96.                         timedOut.incrementAndGet();
  97.                     } else {
  98.                         outcome = new TaskFailure(taskId, cause);
  99.                         failed.incrementAndGet();
  100.                     }
  101.                 } else {
  102.                     outcome = new TaskSuccess(taskId, res);
  103.                     success.incrementAndGet();
  104.                 }
  105.                 storage.put(taskId, outcome);
  106.                 return null;
  107.             }));
  108.         }
  109.  
  110.         CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
  111.         return new ExecutionSummary(tasks.size(), success.get(), failed.get(), timedOut.get(), storage.getSnapshot());
  112.     }
  113.  
  114.     public void shutdown() {
  115.         executor.shutdown();
  116.     }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment