Exercises


[Page 704]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 14.1

Explain the difference between the following pairs of terms:

  1. Blocked and ready.

  2. Priority scheduling and round-robin scheduling.

  3. Producer and consumer.

  4. Monitor and lock.

  5. Concurrent and time slicing.

  6. Mutual exclusion and critical section.

  7. Busy waiting and nonbusy waiting.

Exercise 14.2

Fill in the blanks.

  1. _________ happens when a CPU's time is divided among several different threads.

  2. A method that should not be interrupted during its execution is known as a _________.

  3. The scheduling algorithm in which each thread gets an equal portion of the CPU's time is known as _________.

  4. The scheduling algorithm in which some threads can preempt other threads is known as _________.

  5. A _________ is a mechanism that enforces mutually exclusive access to a synchronized method.

  6. A thread that performs an I/O operation may be forced into the _________ state until the operation is completed.

Exercise 14.3

Describe the concept of time slicing as it applies to CPU scheduling.

Exercise 14.4

What's the difference in the way concurrent threads would be implemented on a computer with several processors and on a computer with a single processor?

Exercise 14.5

Why are threads put into the blocked state when they perform an I/O operation?

Exercise 14.6

What's the difference between a thread in the sleep state and a thread in the ready state?

Exercise 14.7

Deadlock is a situation that occurs when one thread is holding a resource that another thread is waiting for, while the other thread is holding a resource that the first thread is waiting for. Describe how deadlock can occur at a four-way intersection with cars entering from each branch. How can it be avoided?

Exercise 14.8

Starvation can occur if one thread is repeatedly preempted by other threads. Describe how starvation can occur at a four-way intersection and how it can be avoided.

Exercise 14.9

Use the Runnable interface to define a thread that repeatedly generates random numbers in the interval 2 through 12.

Exercise 14.10

Create a version of the Bakery program that uses two clerks to serve customers.


[Page 705]
Exercise 14.11

Modify the Numbers program so that the user can interactively create NumberThreads and assign them a priority. Modify the NumberThreads so that they print their numbers indefinitely (rather than for a fixed number of iterations). Then experiment with the system by observing the effect of introducing threads with the same, lower, or higher priority. How do the threads behave when they all have the same priority? What happens when you introduce a higher-priority thread into the mix? What happens when you introduce a lower-priority thread into the mix?

Exercise 14.12

Create a bouncing-ball simulation in which a single ball (thread) bounces up and down in a vertical line. The ball should bounce off the bottom and top of the enclosing frame.

Exercise 14.13

Modify the simulation in the preceding exercise so that more than one ball can be introduced. Allow the user to introduce new balls into the simulation by pressing the space bar or clicking the mouse.

Exercise 14.14

Modify your solution to the preceding problem by having the balls bounce off the wall at a random angle.

Exercise 14.15

Challenge: One type of producer/consumer problem is the reader/writer problem. Create a subclass of JTextField that can be shared by threads, one of which writes a random number to the text field, and the other reads the value in the text field. Coordinate the two threads so that the overall effect of the program will be to print the values from 0 to 100 in the proper order. In other words, the reader thread should not read a value from the text field until there is a value to be read. The writer thread should not write a value to the text field until the reader has read the previous value.

Exercise 14.16

Challenge: Create a streaming banner thread that moves a simple message across a panel. The message should repeatedly enter at the left edge of the panel and exit from the right edge. Design the banner as a subclass of JPanel and have it implement the Runnable interface. That way it can be added to any user interface. One of its constructors should take a String argument that lets the user set the banner's message.

Exercise 14.17

Challenge: Create a slide-show applet that repeatedly cycles through an array of images. The action of displaying the images should be a separate thread. The applet thread should handle the user interface. Give the user some controls that let it pause, stop, start, speed up, and slow down the images.

Exercise 14.18

Challenge: Create a horse-race simulation, using separate threads for each of the horses. The horses should race horizontally across the screen, with each horse having a different vertical coordinate. If you don't have good horse images to use, just make each horse a colored polygon or some other shape. Have the horses implement the Drawable interface introduced in Chapter 8.

Exercise 14.19

Challenge: Create a multithreaded digital-clock application. One thread should keep time in an endless while loop. The other thread should be responsible for updating the screen each second.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net