Task Scheduler Java Program

Write a scheduler in java which lists the tasks in the priority order based on the priority of the task and the duration it takes to complete.

import java.util.PriorityQueue;

public class TaskScheduler {
    public static void main(String[] args) {
        // Create a priority queue to store tasks
        PriorityQueue<Task> taskQueue = new PriorityQueue<>();

        // Add tasks to the priority queue
        taskQueue.add(new Task("Task 1", 2, 15));
        taskQueue.add(new Task("Task 2", 1, 10));
        taskQueue.add(new Task("Task 3", 2, 20));

        // Process tasks in order of priority
        while (!taskQueue.isEmpty()) {
            Task task = taskQueue.poll();
            System.out.println("Processing task: " + task.getName());
        }
    }
}

class Task implements Comparable<Task> {
    private String name;
    private int priority;
    private int duration;

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

    public String getName() {
        return name;
    }

    public int getPriority() {
        return priority;
    }

    public int getDuration() {
        return duration;
    }

    @Override
    public int compareTo(Task other) {
        int priorityComparison = Integer.compare(this.priority, other.priority);
        if (priorityComparison != 0) {
            return priorityComparison;
        } else {
            return Integer.compare(this.duration, other.duration);
        }
    }
}

2 comments

  • I appreciate your website; however, I would like to bring to your attention that a number of your postings contain misspelled words. While I find it extremely bothersome to provide the truth, I will definitely revisit your site.

  • Moses3974

    The Beatles – легендарная британская рок-группа, сформированная в 1960 году в Ливерпуле. Их музыка стала символом эпохи и оказала огромное влияние на мировую культуру. Среди их лучших песен: “Hey Jude”, “Let It Be”, “Yesterday”, “Come Together”, “Here Comes the Sun”, “A Day in the Life”, “Something”, “Eleanor Rigby” и многие другие. Их творчество отличается мелодичностью, глубиной текстов и экспериментами в звуке, что сделало их одной из самых влиятельных групп в истории музыки. Музыка 2024 года слушать онлайн и скачать бесплатно mp3.

Leave a Reply