Advertisement
Jaydeep999997

Cont Task Scheduler

Nov 27th, 2024
110
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | Source Code | 1 0
  1.  
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.atomic.AtomicBoolean;
  5.  
  6. public interface Task {
  7.     void execute() throws Exception; // Simulate task execution, may throw an exception
  8. }
  9.  
  10. public class TaskScheduler {
  11.     private final Queue<Task> taskQueue = new LinkedList<>();
  12.     private final AtomicBoolean isFailed = new AtomicBoolean(false);
  13.     private final Object lock = new Object(); // Synchronization lock
  14.     private final TaskAPI taskAPI; // Simulates the external API to fetch dependent tasks
  15.  
  16.     public TaskScheduler(TaskAPI taskAPI) {
  17.         this.taskAPI = taskAPI;
  18.     }
  19.  
  20.     // Method to start the scheduler with initial tasks
  21.     public void start(List<Task> initialTasks) {
  22.         synchronized (lock) {
  23.             taskQueue.addAll(initialTasks);
  24.             lock.notify(); // Wake up the main thread if waiting
  25.         }
  26.  
  27.         Thread mainThread = new Thread(this::mainScheduler);
  28.         mainThread.start();
  29.     }
  30.  
  31.     // Main scheduler thread
  32.     private void mainScheduler() {
  33.         while (true) {
  34.             Task task;
  35.  
  36.             // Fetch the next task, wait if the queue is empty
  37.             synchronized (lock) {
  38.                 while (taskQueue.isEmpty() && !isFailed.get()) {
  39.                     try {
  40.                         lock.wait(); // Wait for new tasks
  41.                     } catch (InterruptedException e) {
  42.                         Thread.currentThread().interrupt();
  43.                         return;
  44.                     }
  45.                 }
  46.  
  47.                 if (isFailed.get() || taskQueue.isEmpty()) {
  48.                     return; // Exit if there's a failure or no tasks
  49.                 }
  50.  
  51.                 task = taskQueue.poll(); // Dequeue the task
  52.             }
  53.  
  54.             // Schedule the task
  55.             Thread workerThread = new Thread(() -> {
  56.                 try {
  57.                     task.execute(); // Execute the task
  58.  
  59.                     // Fetch dependent tasks after completion
  60.                     List<Task> dependentTasks = taskAPI.getDependentTasks(task);
  61.  
  62.                     // Add new tasks to the queue
  63.                     synchronized (lock) {
  64.                         taskQueue.addAll(dependentTasks);
  65.                         lock.notify(); // Notify the main thread
  66.                     }
  67.                 } catch (Exception e) {
  68.                     isFailed.set(true); // Mark failure
  69.                     synchronized (lock) {
  70.                         lock.notify(); // Wake up the main thread to exit
  71.                     }
  72.                 }
  73.             });
  74.  
  75.             workerThread.start();
  76.         }
  77.     }
  78. }
  79.  
  80. public class TaskAPI {
  81.     public List<Task> getDependentTasks(Task task) {
  82.         // Simulate fetching dependent tasks from the API
  83.         // In reality, this would query a database or external service
  84.         return new ArrayList<>(); // Return an empty list for simplicity
  85.     }
  86. }
  87.  
  88.  
  89. public class Main {
  90.     public static void main(String[] args) {
  91.         TaskAPI taskAPI = new TaskAPI();
  92.  
  93.         Task task1 = () -> {
  94.             System.out.println("Executing Task 1");
  95.             Thread.sleep(1000); // Simulate work
  96.         };
  97.  
  98.         Task task2 = () -> {
  99.             System.out.println("Executing Task 2");
  100.             Thread.sleep(1000);
  101.         };
  102.  
  103.         Task task3 = () -> {
  104.             System.out.println("Executing Task 3");
  105.             Thread.sleep(1000);
  106.         };
  107.  
  108.         TaskScheduler scheduler = new TaskScheduler(taskAPI);
  109.         scheduler.start(List.of(task1, task2, task3));
  110.     }
  111. }
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement