Abhishek Herle
Abhishek Herle

Reputation: 71

Simulation in java using priority queues

I've been asked to simulate the events for a store with one counter in java using a priority queue. Where a person is being served and if someone arrives during this time i increment the number of people in the queue.I figured out that I have to use Comparator but so far it seems like i can only use comparator for sorting and not for queuing and DE-queuing events.

Upvotes: 0

Views: 1913

Answers (2)

pjs
pjs

Reputation: 19855

If you are using a discrete event modeling perspective, you use a priority queue to schedule the sequence of events that drive the system. You can find a tutorial paper on how to do this, along with a Java implementation for a single server queue with exponential interarrival and service times, in the Winter Simulation Conference paper archives.

Upvotes: 2

Sachin Thapa
Sachin Thapa

Reputation: 3709

Problem statement you have mentioned you can implement it using a simple Queue, I could think of following design.

  1. Queue - Which will hold list of persons.
  2. Thread1 - Reads arriving persons and add Person into Queue
  3. Thread2 - Removes from the Queue for processing
  4. Size of Queue will give you number of persons in Queue

If you are working on collections and you need to sort objects then you need a compartor, but given the problem statement, looks like there is not need for sorting.

Cheers !!

Upvotes: -1

Related Questions