WikiGalaxy

Personalize

Priority Queue Applications

Task Scheduling

Priority queues are widely used in scheduling algorithms where tasks need to be processed based on their priority levels. For instance, in operating systems, tasks with higher priority should be executed before those with lower priority.


import java.util.PriorityQueue;
class TaskScheduler {
    public static void main(String[] args) {
        PriorityQueue taskQueue = new PriorityQueue<>();
        taskQueue.add(new Task("Critical Update", 1));
        taskQueue.add(new Task("Background Sync", 5));
        taskQueue.add(new Task("User Request", 3));
        
        while (!taskQueue.isEmpty()) {
            System.out.println(taskQueue.poll().name);
        }
    }
}
class Task implements Comparable {
    String name;
    int priority;
    
    Task(String name, int priority) {
        this.name = name;
        this.priority = priority;
    }
    
    public int compareTo(Task t) {
        return Integer.compare(this.priority, t.priority);
    }
}
    

Console Output:

Critical Update

User Request

Background Sync

Network Packet Management

Packet Prioritization

In network systems, priority queues are used to manage packets based on their importance. High-priority packets, like those for live video streaming, are processed before lower-priority ones to ensure quality of service.


import java.util.PriorityQueue;
class NetworkManager {
    public static void main(String[] args) {
        PriorityQueue packetQueue = new PriorityQueue<>();
        packetQueue.add(new Packet("Video Stream", 1));
        packetQueue.add(new Packet("Email", 4));
        packetQueue.add(new Packet("Web Browsing", 3));
        
        while (!packetQueue.isEmpty()) {
            System.out.println(packetQueue.poll().type);
        }
    }
}
class Packet implements Comparable {
    String type;
    int priority;
    
    Packet(String type, int priority) {
        this.type = type;
        this.priority = priority;
    }
    
    public int compareTo(Packet p) {
        return Integer.compare(this.priority, p.priority);
    }
}
    

Console Output:

Video Stream

Web Browsing

Email

Dijkstra's Shortest Path Algorithm

Graph Traversal

Priority queues are integral to Dijkstra's algorithm, which finds the shortest path in a graph. Nodes are prioritized based on their current shortest distance from the source node.


import java.util.PriorityQueue;
import java.util.HashMap;
import java.util.Map;

class Graph {
    private Map nodes = new HashMap<>();

    void addNode(String node, int distance) {
        nodes.put(node, distance);
    }

    void dijkstra() {
        PriorityQueue> pq = new PriorityQueue<>(
            Map.Entry.comparingByValue()
        );
        pq.addAll(nodes.entrySet());
        
        while (!pq.isEmpty()) {
            Map.Entry entry = pq.poll();
            System.out.println("Node: " + entry.getKey() + ", Distance: " + entry.getValue());
        }
    }
}

class Dijkstra {
    public static void main(String[] args) {
        Graph graph = new Graph();
        graph.addNode("A", 0);
        graph.addNode("B", 10);
        graph.addNode("C", 5);
        
        graph.dijkstra();
    }
}
    

Console Output:

Node: A, Distance: 0

Node: C, Distance: 5

Node: B, Distance: 10

Event-Driven Simulation

Event Scheduling

In event-driven simulations, priority queues are used to schedule events based on their occurrence time, allowing the simulation to process events in the correct chronological order.


import java.util.PriorityQueue;

class EventSimulation {
    public static void main(String[] args) {
        PriorityQueue eventQueue = new PriorityQueue<>();
        eventQueue.add(new Event("Start Simulation", 0));
        eventQueue.add(new Event("End Simulation", 10));
        eventQueue.add(new Event("Mid Simulation", 5));
        
        while (!eventQueue.isEmpty()) {
            System.out.println(eventQueue.poll().description);
        }
    }
}

class Event implements Comparable {
    String description;
    int time;
    
    Event(String description, int time) {
        this.description = description;
        this.time = time;
    }
    
    public int compareTo(Event e) {
        return Integer.compare(this.time, e.time);
    }
}
    

Console Output:

Start Simulation

Mid Simulation

End Simulation

AI Pathfinding

A* Algorithm

Priority queues are crucial in AI pathfinding algorithms like A*. Nodes are prioritized based on a cost function that estimates the shortest path to the goal.


import java.util.PriorityQueue;
import java.util.HashMap;
import java.util.Map;

class AStarPathfinding {
    private Map nodes = new HashMap<>();

    void addNode(String node, int cost) {
        nodes.put(node, cost);
    }

    void findPath() {
        PriorityQueue> pq = new PriorityQueue<>(
            Map.Entry.comparingByValue()
        );
        pq.addAll(nodes.entrySet());
        
        while (!pq.isEmpty()) {
            Map.Entry entry = pq.poll();
            System.out.println("Node: " + entry.getKey() + ", Cost: " + entry.getValue());
        }
    }
}

class AStar {
    public static void main(String[] args) {
        AStarPathfinding pathfinding = new AStarPathfinding();
        pathfinding.addNode("Start", 0);
        pathfinding.addNode("Middle", 15);
        pathfinding.addNode("Goal", 5);
        
        pathfinding.findPath();
    }
}
    

Console Output:

Node: Start, Cost: 0

Node: Goal, Cost: 5

Node: Middle, Cost: 15

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025