We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Applications of Queue
Last updated:
A queue hands out elements in the order they arrived: FIFO, First In, First Out. Elements join at the rear (tail) and leave from the front (head). Wherever work arrives faster than it can be handled, or must be handled fairly in arrival order, a queue is the natural way to hold it.
Applications of Queue Data Structure
Some common places where queues are used:
CPU and task scheduling: the operating system keeps ready processes in a queue; round-robin scheduling runs the process at the front for a time slice and then moves it to the back. (When tasks must run by importance instead of arrival time, a priority queue is used.)
Printer spooling: print jobs wait in a queue and are printed in the order they were sent.
Web servers and thread pools: incoming requests or tasks wait in a queue until a worker thread is free. In Java, ThreadPoolExecutor holds waiting tasks in a BlockingQueue.
Message queues between services: systems such as RabbitMQ and Apache Kafka let one service hand work to another without waiting for it, as in an e-commerce order pipeline from checkout to payment to shipping.
Network buffers: routers and network cards hold packets in queues until they can be forwarded; when a buffer is full, new packets may be dropped.
Streaming and I/O buffers: keyboard input, audio and video data are kept in (often circular) buffers and consumed in arrival order.
Breadth-first search: BFS uses a queue to visit a graph level by level, which is how it finds shortest paths in unweighted graphs.
Batch processing: jobs such as report generation or image rendering are queued and processed one after another.
Real-world waiting lines: call centres, ticket counters and clinic appointment systems serve people in the order they arrived.
Frequently Asked Questions (FAQ)
Q1. Why are queues used in operating system scheduling?
Processes waiting for the CPU are kept in a ready queue. In round-robin scheduling, the process at the front runs for a time slice and then moves to the back of the queue, so every process gets a fair share of CPU time.
Q2. How is a queue used in breadth-first search?
BFS puts the start node in a queue, then repeatedly removes a node and adds its unvisited neighbours. Because the queue is FIFO, nodes are visited level by level, which is why BFS finds the shortest path in an unweighted graph.
Q3. Write a Java program that simulates a printer queue.
PrinterQueue.java
import java.util.ArrayDeque;
import java.util.Queue;
public class PrinterQueue {
public static void main(String[] args) {
Queue<String> jobs = new ArrayDeque<>();
jobs.offer("report.pdf");
jobs.offer("invoice.docx");
jobs.offer("photo.png");
while (!jobs.isEmpty()) {
System.out.println("Printing " + jobs.poll());
}
}
}
Q4. How are queues used in web servers?
Incoming requests are placed in a queue, and a pool of worker threads takes them one at a time. Java's ThreadPoolExecutor uses a BlockingQueue to hold tasks that are waiting for a free thread.
Q5. What is a message queue in microservices?
A message queue, such as RabbitMQ or Apache Kafka, lets one service send messages that another service processes later. The sender does not wait for the receiver, which makes the system more resilient and lets each side scale independently.
Q6. Write a Java program that finds the shortest path length in a grid using a queue.
GridShortestPath.java
import java.util.ArrayDeque;
import java.util.Queue;
public class GridShortestPath {
static int shortestPath(int[][] grid) { // 0 = open cell, 1 = wall
int rows = grid.length, cols = grid[0].length;
boolean[][] seen = new boolean[rows][cols];
Queue<int[]> queue = new ArrayDeque<>();
queue.offer(new int[]{0, 0, 0}); // row, column, distance
seen[0][0] = true;
int[][] moves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!queue.isEmpty()) {
int[] cell = queue.poll();
if (cell[0] == rows - 1 && cell[1] == cols - 1) {
return cell[2];
}
for (int[] m : moves) {
int r = cell[0] + m[0], c = cell[1] + m[1];
if (r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == 0 && !seen[r][c]) {
seen[r][c] = true;
queue.offer(new int[]{r, c, cell[2] + 1});
}
}
}
return -1; // no path
}
public static void main(String[] args) {
int[][] grid = {
{0, 0, 1},
{1, 0, 1},
{1, 0, 0}
};
System.out.println(shortestPath(grid)); // 4
}
}
Q7. How are queues used in networking?
Routers and network cards store incoming packets in queues (buffers) until they can be forwarded. If packets arrive faster than they can be sent, the queue fills up and new packets may be dropped.
Q8. How are queues used for buffering streaming data?
Video players and keyboard drivers keep incoming data in a queue (often a circular buffer). Data is consumed in arrival order, which keeps playback smooth even when data arrives in bursts.
Q9. Write a Java program that prints the first non-repeating character in a stream.
FirstNonRepeating.java
import java.util.ArrayDeque;
import java.util.Queue;
public class FirstNonRepeating {
public static void main(String[] args) {
String stream = "aabcbd";
int[] counts = new int[26];
Queue<Character> queue = new ArrayDeque<>();
for (char c : stream.toCharArray()) {
counts[c - 'a']++;
queue.offer(c);
while (!queue.isEmpty() && counts[queue.peek() - 'a'] > 1) {
queue.poll(); // drop characters that repeated
}
System.out.print(queue.isEmpty() ? "# " : queue.peek() + " ");
}
// Output: a # b b c c
}
}
Q10. Which queue type fits each application?
Simple FIFO processing: a regular queue (ArrayDeque).
Fixed-size buffers: a circular queue.
Urgent tasks first: a priority queue.
Sliding-window problems and undo with redo: a deque.