Advertisement
Jaydeep999997

Cont Task Scheduler 2

Nov 27th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | Source Code | 0 0
  1. import java.io.IOException;
  2. import java.util.*;
  3. import java.util.concurrent.atomic.AtomicBoolean;
  4.  
  5. abstract class Task {
  6.     abstract String taskId();
  7.  
  8.     abstract void doWork() throws IOException;
  9. }
  10.  
  11. class SchedulerWithQueue {
  12.     private final Queue<Task> taskQueue = new LinkedList<>();
  13.     private final Set<Task> finishedTasks = new HashSet<>();
  14.     private final Set<Task> runningTasks = new HashSet<>();
  15.     private final AtomicBoolean failureDetected = new AtomicBoolean(false);
  16.     private final Object lock = new Object();
  17.  
  18.     // Start execution
  19.     public void execute() throws InterruptedException {
  20.         synchronized (lock) {
  21.             Collection<Task> initialTasks = nextTasks(List.of());
  22.             if (initialTasks.isEmpty()) {
  23.                 return; // No tasks to execute
  24.             }
  25.  
  26.             taskQueue.addAll(initialTasks);
  27.             lock.notifyAll(); // Notify waiting threads
  28.         }
  29.  
  30.         // Scheduler thread
  31.         while (true) {
  32.             Task task;
  33.  
  34.             // Fetch a task from the queue
  35.             synchronized (lock) {
  36.                 while (taskQueue.isEmpty() && !shouldStop()) {
  37.                     lock.wait(); // Wait for new tasks or stop condition
  38.                 }
  39.  
  40.                 if (shouldStop()) {
  41.                     break; // Exit loop if no more tasks or failure
  42.                 }
  43.  
  44.                 task = taskQueue.poll();
  45.                 runningTasks.add(task);
  46.             }
  47.  
  48.             // Run the task in a new thread
  49.             Runnable runnable = createCustomRunnable(task);
  50.             new Thread(runnable).start();
  51.         }
  52.     }
  53.  
  54.     // Determine when to stop the scheduler
  55.     private synchronized boolean shouldStop() {
  56.         return (taskQueue.isEmpty() && runningTasks.isEmpty() && nextTasks(finishedTasks).isEmpty())
  57.                 || failureDetected.get();
  58.     }
  59.  
  60.     // Create a runnable to execute a task
  61.     private Runnable createCustomRunnable(Task task) {
  62.         return () -> {
  63.             try {
  64.                 if (!failureDetected.get()) {
  65.                     task.doWork();
  66.                 }
  67.                 onTaskComplete(task);
  68.             } catch (Exception e) {
  69.                 failureDetected.set(true); // Mark failure
  70.             } finally {
  71.                 synchronized (lock) {
  72.                     lock.notifyAll(); // Notify scheduler of task completion
  73.                 }
  74.             }
  75.         };
  76.     }
  77.  
  78.     // Handle task completion
  79.     private void onTaskComplete(Task task) {
  80.         synchronized (lock) {
  81.             runningTasks.remove(task);
  82.             finishedTasks.add(task);
  83.  
  84.             if (!failureDetected.get()) {
  85.                 // Fetch new tasks if no failure
  86.                 Collection<Task> dependentTasks = nextTasks(finishedTasks);
  87.                 for (Task dependentTask : dependentTasks) {
  88.                     if (!runningTasks.contains(dependentTask) && !finishedTasks.contains(dependentTask)) {
  89.                         taskQueue.add(dependentTask);
  90.                     }
  91.                 }
  92.                 lock.notifyAll(); // Notify scheduler of new tasks
  93.             }
  94.         }
  95.     }
  96.  
  97.     // Simulated API for fetching dependent tasks
  98.     private Collection<Task> nextTasks(Collection<Task> completedTasks) {
  99.         // Assume this method is implemented
  100.         return Collections.emptyList();
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement