DEV Community

Abhishek Kumar
Abhishek Kumar

Posted on

Implement a small program using PriorityQueue to simulate a job scheduler.

Here’s an implementation of a small program using a PriorityQueue to simulate a job scheduler. In this example, jobs have priorities, and the job scheduler processes the highest-priority jobs first (lower numbers represent higher priorities).

Java Program: Job Scheduler using PriorityQueue

import java.util.PriorityQueue;
import java.util.Comparator;

// Class representing a Job with a priority and a name
class Job {
    private String name;
    private int priority;

    public Job(String name, int priority) {
        this.name = name;
        this.priority = priority;
    }

    public String getName() {
        return name;
    }

    public int getPriority() {
        return priority;
    }

    @Override
    public String toString() {
        return "Job[name=" + name + ", priority=" + priority + "]";
    }
}

public class JobScheduler {
    public static void main(String[] args) {
        // Create a PriorityQueue with a custom comparator to order jobs by priority
        PriorityQueue<Job> jobQueue = new PriorityQueue<>(new Comparator<Job>() {
            @Override
            public int compare(Job j1, Job j2) {
                return Integer.compare(j1.getPriority(), j2.getPriority());
            }
        });

        // Add jobs to the PriorityQueue (lower priority number means higher importance)
        jobQueue.add(new Job("Database backup", 3));
        jobQueue.add(new Job("Send report to manager", 1));
        jobQueue.add(new Job("System update", 5));
        jobQueue.add(new Job("Email notifications", 2));

        // Simulate processing the jobs
        System.out.println("Processing jobs in priority order:");
        while (!jobQueue.isEmpty()) {
            Job job = jobQueue.poll(); // Retrieves and removes the highest-priority job
            System.out.println("Processing: " + job);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Job Class: Represents a job with a name and priority. The lower the priority value, the higher its importance (e.g., a priority of 1 is higher than a priority of 5).
  • PriorityQueue: The job queue is a PriorityQueue that orders jobs based on their priority. We define a custom comparator to ensure that jobs with lower priority numbers are processed first.
  • Main Logic:
    • Jobs are added to the queue with different priority levels.
    • The jobs are processed in priority order using the poll() method, which retrieves and removes the highest-priority job (smallest number).

Output:

Processing jobs in priority order:
Processing: Job[name=Send report to manager, priority=1]
Processing: Job[name=Email notifications, priority=2]
Processing: Job[name=Database backup, priority=3]
Processing: Job[name=System update, priority=5]
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • PriorityQueue ensures that jobs are processed based on priority.
  • Comparator is used to define the order in which jobs are scheduled (higher priority jobs are processed first).
  • poll() retrieves and removes the highest-priority job from the queue.

This simple job scheduler demonstrates how a PriorityQueue can be used to handle tasks based on their priority in a real-world scenario like system job scheduling.

Top comments (0)