Advertisement
nazar_art

Task for parallel processing

Feb 2nd, 2025 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | Source Code | 0 0
  1. import java.util.List;
  2. import java.util.concurrent.CompletableFuture;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         final BusinessEntity first = new BusinessEntity(1001, List.of(new Action(2, "Action2"), new Action(1, "Action1"), new Action(3, "Action3")));
  9.         final BusinessEntity second = new BusinessEntity(1002, List.of(new Action(4, "Action4"), new Action(6, "Action6"), new Action(5, "Action5")));
  10.  
  11.         // todo: run 2 above business entities in parallel + each has a list of actions -> sort that list by priority (1, 2, 3...) and run sequentially according to priority with processAction() -> 1 - first, 2 - second...
  12.        
  13.     }
  14.  
  15.     public static CompletableFuture<String> processAction(final int entityId, final int priorityId, final String actionName) {
  16.         return CompletableFuture.supplyAsync(() -> {
  17.             System.out.printf("process entityId: %d, priorityId: %d, actionName: %s, in thread: %s%n",
  18.                     entityId, priorityId, actionName, Thread.currentThread().getName());
  19.             return "DONE";
  20.         });
  21.     }
  22.  
  23.     public static class BusinessEntity {
  24.         private final int entityId;
  25.         private final List<Action> actions;
  26.  
  27.         public BusinessEntity(int entityId, List<Action> actions) {
  28.             this.entityId = entityId;
  29.             this.actions = actions;
  30.         }
  31.     }
  32.  
  33.     public static class Action {
  34.         private final int priorityId;
  35.         private final String actionName;
  36.  
  37.         public Action(int priorityId, String actionName) {
  38.             this.priorityId = priorityId;
  39.             this.actionName = actionName;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement