CPSC 170 Lab 12: Queues

Implementing Queues

The file Queue.java contains an interface for the queue abstract data type. Create a class that implements the Queue interface using either an array or a linked implementation. Note the following:

Feel free to use your stack implementations from the last lab to help with creating a queue implementation. When you think that you have a working queue, test the class using the program in the file QueueTest.java. Note that the initialization of the queue object in this program must be completed before it will run.

Threads

All of the programming we have done up until now has consisted of a single process. A process is a program that has its own memory that other processes can not access. It is possible to create processes that interact with each other by passing data between them. Instead of creating two separate processes that communicate over a network connection, which we will do next week, we are going to use threads. Threads are similar to processes except that threads are contained inside of processes and so they share the memory of the process. In Java threads are created using the Thread class.

The file ThreadTest.java contains an example of how to use threads. In this program the main thread (the one that executes the main method) creates the other thread and then begins a loop that takes user input via the command line. The second thread is contained in the run method of the PrintLoop class. Note that the PrintLoop class implements Runnable, which is required by the constructor for the thread. The run method does not begin execution until the start method is called. Once the start method is called, the two methods are running concurrently. The second thread stops periodically (for 1000 milliseconds) every time the sleep method is called. The second thread stops executing when it is interrupted by the first thread calling the interrupt method. Run the program and make sure that you understand how this program and threads work before proceeding.

The files Mandelbrot.java and InteractiveMandelbrot.java contain a program that allows the user to interactively render high resolution images of the Mandelbrot set to an image file. Run the program to see how it works. Look at the code for the program as well. Notice that the program generates low resolution images (which can be created quickly) so that the program does not become unresponsive when the user clicks to zoom in. When the user presses the space bar it generates a high resolution image. While the program generates the high resolution image it is not possible to zoom in.

Fix this program to allow the user to continue to interact with the program while it is generating the high resolution image. Hint: if a second thread creates the high resolution image then the main thread can continue to handle mouse events and render low resolution images.

Extra

You can use this program to generate movies of zooming into a fractal. Use the interactive program to zoom into a cool looking, deep part of the fractal. Then, instead of just generating an high resolution image of the current screen, create a collection of images, each just a little closer to the end image. Do this by interpolating between the initial dimensions and the final dimensions by an increasing fraction, each time rendering the result to an image file. These image files can then be turned into a movie.

Hand In: Tar your lab directory and e-mail it to your instructor with cpsc170 lab12 in the subject.