Course Links

Exercises

Resources

External

In computer systems, queues are commonly used anywhere there is a mismatch in speeds between the production of some type of data and its consumption. The queue allows each process to proceed at its own pace, and ensures that each item will be handled in the order in which it was created. This lab simulates such a situation.

Without Queues

Download the files NoQueueGUI.java and JKeyProcess.java. When you compile them and run NoQueueGUI, you will see a simple GUI window consisting of a box with a label under it. The box is the visual representation of JKeyProcess, which is intended to simulate a (slow) processing module for keystrokes. It can only handle one key stroke at a time, and any additional strokes pressed during handling of the first will be ignored. Try typing a word, and note how inconvenient the delay is. For purposes of this lab, you should treat JKeyProcess as something that cannot be modified; instead, we'll have to work around its deficiencies by redesigning the rest of the program.

Important note for Windows users connecting via Xming: in order for the program to receive keystrokes, you need to start Xming in single-window mode. Make sure you have exited out of Xming (right click the icon in the status bar in the lower left of the screen), then restart it using XLaunch. Select "One Window" in the first configuration screen and the defaults on all the rest.

Everything would work much more smoothly if extra keystrokes could be buffered in a queue, so that the processing module could handle them in order when it is ready. Your task in this lab is to make this change. To make the queue behavior visible, the class JQueue is designed to show the current contents of your queue at any given time.

Specifics

To help you complete this task during the lab period, here are some suggestions:

  1. Make a copy of NoQueueGUI.java, called QueueGUI.java. This is the file you will be modifying. Also download a copy of JQueue.java; you won't need to change this.
  2. Add a queue field to QueueGUI. This can be any class that implements the Queue<Character> interface. You will use this field to store extra keystrokes, and feed them to the processor when it is ready. Also add a JQueue field, so that the contents of your queue can be displayed visually.
  3. Inside createComponents(), you should initialize your two new fields. Note that you must pass the queue you create as an argument to the JQueue constructor, so that it knows what to display. Once your JQueue is set up, add it to the content pane in the BorderLayout.EAST position.
  4. Rewrite the keyTyped() handler method. Instead of always trying to pass the keystroke to the processor, it should check first whether the processor is busy using the isBusy method. If it is, the keystroke should be added to the queue. Also, whenever the queue is updated you should call repaint() on the JQueue.
  5. We also need a way to get the keystrokes off the queue and into the key processor when it is ready. The key processor can notify our GUI manager when it is free if we register an action listener with it. So go ahead and create a nested action listener class. When triggered, it should move one keystroke off the queue and into the key processor. Don't forget to repaint()!
  6. Then add code in createComponents to initialize an instance of your new listener and register it with the keystroke processor.
  7. That's it! Test out your program and make sure it works.
  8. If you have time and feel like it, you can implement your own MyQueue<E> to replace the one from the standard Collections classes. If you do, you should base it on Stack<E> and your class notes. You will have to rewrite JQueue.java somewhat to work with the new class.

To Submit