---
title: "Java simulation of single server queue"
subject: "Simulation and Modeling"
chapter: 9
key: "semester_8/simulation_and_modeling:java-simulation-single-server"
source: https://www.bctengineer.com/q/semester_8/simulation_and_modeling/java-simulation-single-server
site: BCT Engineering
---

# Java simulation of single server queue

Simulation and Modeling · Chapter 9: Simulation software

- Asked in: 2074 Bhadra, 6 marks; 2073 Bhadra, 6 marks; 2073 Magh, 6 marks; 2072 Ashwin, 6 marks; 2071 Bhadra, 6 marks (asked 5 times)
- Video: [Simulation in Java // Chap 9 // Simulation and Modelling IOE](https://www.youtube.com/watch?v=8I3PQHYFTQ4) — Suyan Shrestha
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_8/simulation_and_modeling/java-simulation-single-server
- Whole chapter: https://www.bctengineer.com/md/semester_8/simulation_and_modeling/chapter-9

## Question

Explain the structure of simulation in Java (single server queue model) with example.

## Short answer

A Java discrete-event simulation uses a priority queue for events, advancing clock to the next arrival or departure. It tracks server status and queue, updating statistics like average queue length and utilization. Example: exponential inter-arrival and service times, run for 1000 customers, output average delay, queue length, utilization.

## Answer

A Java discrete-event simulation of a single-server queue (such as a bank with one teller) is built from a **simulation clock, an event list, state variables, statistics and an event-driven main loop**. The model is event-scheduling based: the clock jumps from one event to the next imminent event.

#### Structure
1. **Classes**
   - `Event`: type (arrival or departure) and time. It implements `Comparable` so the earliest event comes first.
   - `Sim` (main class): clock, event list (a `PriorityQueue<Event>`), customer queue (a `LinkedList`), server status, statistics.
   - Random generators: `java.util.Random` with a seed, to produce exponential inter-arrival and service times.
2. **State variables**: server busy (boolean), queue length, clock.
3. **Statistics**: total delay, customers served, area under the queue-length curve, total busy time.
4. **Methods**: `initialize()`, `arrival()`, `departure()`, `report()`, and the `main` loop.

#### Flow
```text
initialize: clock=0, schedule 1st arrival
       |
  +--> take next event from list
  |    advance clock, update statistics
  |    arrival or departure? -> handle
  |    schedule new events
  +--- stop? no
       |yes
     report()
```

#### Example code
```java
import java.util.*;
public class Sim {
  static double clock = 0, lastT = 0, busyT = 0, area = 0, delay = 0;
  static int served = 0; static boolean busy = false;
  static Random r = new Random(1);
  static PriorityQueue<double[]> fel =
      new PriorityQueue<>((a, b) -> Double.compare(a[0], b[0]));
  static LinkedList<Double> q = new LinkedList<>();
  static double expo(double m) { return -m * Math.log(1 - r.nextDouble()); }

  public static void main(String[] a) {
    fel.add(new double[]{expo(4), 0});       // type 0 = arrival
    while (served < 1000) {
      double[] e = fel.poll();
      area += q.size() * (e[0] - clock);
      if (busy) busyT += e[0] - clock;
      clock = e[0];
      if (e[1] == 0) {                       // arrival
        fel.add(new double[]{clock + expo(4), 0});
        if (busy) q.add(clock);
        else { busy = true; fel.add(new double[]{clock + expo(3), 1}); }
      } else {                               // departure
        served++;
        if (q.isEmpty()) busy = false;
        else { delay += clock - q.poll();
               fel.add(new double[]{clock + expo(3), 1}); }
      }
    }
    System.out.println("Avg delay " + delay / served);
    System.out.println("Avg queue " + area / clock);
    System.out.println("Utilisation " + busyT / clock);
  }
}
```
Here mean inter-arrival time is 4 and mean service time is 3 (time units), so theoretically utilisation is 0.75. The program stops after 1000 customers and prints average delay, average queue length and server utilisation.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is the event list implemented in the Java model, and why?
   A. A LinkedList so the first-added event runs first
   B. A PriorityQueue ordered by time so the earliest event is taken first
   C. An array scanned after each event
   D. A HashMap keyed by customer
   Answer: B. A PriorityQueue (with Comparable events) always gives the imminent event with the smallest time. (Key point: Event list)

2. How does the simulation clock advance?
   A. By fixed time steps
   B. By real time
   C. It jumps to the time of the next imminent event
   D. It is reset after each departure
   Answer: C. Event-scheduling models jump the clock from event to event. (Key point: Clock advance)

3. How is the area under the queue-length curve accumulated?
   A. Adding queue size x (event time - clock) before the clock is updated
   B. Adding 1 at each arrival
   C. Adding the delay of each customer
   D. Adding clock at the end
   Answer: A. Before advancing the clock, queue length times elapsed time is added; area/clock gives average queue length. (Key point: Statistics update)

4. With mean inter-arrival time 4 and mean service time 3, what is the theoretical utilisation?
   A. 1.33
   B. 0.5
   C. 0.25
   D. 0.75
   Answer: D. Utilisation = service time / inter-arrival time = 3/4 = 0.75. (Key point: Expected utilisation)

## Related in Simulation and Modeling

- [Define system, model, simulation](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Asystem-model-simulation-define) — Simulation and Modeling, chapter 1
- [CPU simulation with example](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Acpu-simulation-example) — Simulation and Modeling, chapter 10, asked 4×
- [Simulation of computer system serving WWW](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Acomputer-system-www-request) — Simulation and Modeling, chapter 10, asked 2×
- [Analog computer model of the liver](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Aanalog-computer-liver-model) — Simulation and Modeling, chapter 3, asked 5×
- [Iterative process of model calibration](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Acalibration-iterative-process) — Simulation and Modeling, chapter 7, asked 4×
- [Static vs dynamic mathematical model](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Astatic-vs-dynamic-math-model) — Simulation and Modeling, chapter 2, asked 3×

## Related in other subjects

- [Server-side programming and Java Servlet](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Aserver-side-programming-servlet) — Web Technology, chapter 3
- [Petrol pump queue simulation](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apetrol-pump-simulation) — Data Structure and Algorithm, chapter 2
- [Queue; enqueue and dequeue operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aqueue-enqueue-dequeue) — Data Structure and Algorithm, chapter 2, asked 2×
- [Enqueue and dequeue in circular queue](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-queue-insert-delete) — Data Structure and Algorithm, chapter 2, asked 2×
