In the rest of this post, we’ll build this system as described above, and see if we can fix the flaw in it. We will use Clojure to write the solution. Clojure’s concurrency primitives are fantastic, and helped me model this system in an elegant and readable way. As we go along, I’ll explain these primitives in brief. Eric Normand has written an excellent guide to understanding concurrency primitives in Clojure, and Rich Hickey has a great talk about this as well. I highly recommend both these resources to the interested reader. The focus of this post will be on using these tools to implement a non-trivial system.

So what is the problem with the system?

First, let’s get the flaw out of the way. The problem with this system is that the applicant has no idea when he’ll be scheduled with an agent. He must keep staring at the display board. For example, if you are N-30 and stage A took a particularly long time for you, others have moved past you to the next stage (B and beyond). The display board may read that N-41, N-42 etc are at counters B-1, B-2 etc. However, this does not mean that N-30 will show up next. The display board may go all the way to N-60 before N-30 shows up. As there is no certainty about when your number will show up, you have no option but to continuously stare at the board. This gets really irritating after a while.

Here are some ways to deal with this problem:

Instead of using simple queues between the stages, use a priority queue. This means that even if N-30 took a long time on stage A, and the board had moved on to N-40s and above for stage B, as soon as N-30 is done with stage A he will be moved to the top of the queue for B. In this way, the applicant can look at the board and always tell whether he’ll be up next or not. Generate new token numbers between each stage. This will give the applicant a clear idea of the number of people ahead of him at any given point in time. Provide the person with a new display board, where he can enter his token and see where he is in the queue for his stage.

We will see these in action in our simulation program.