Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 14.9 Chapter Exercises

Note: For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.}
  1. Explain the difference between the following pairs of terms:
    1. 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.
    2. Describe the concept of time slicing as it applies to CPU scheduling.
    3. 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?
    4. Why are threads put into the blocked state when they perform an I/O operation?
    5. What’s the difference between a thread in the sleep state and a thread in the ready state?
    6. 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?
    7. 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.
    8. Use the Runnable interface to define a thread that repeatedly generates random numbers in the interval 2 through 12.
    9. Create a version of the Bakery program that uses two clerks to serve customers.
    10. Modify the Numbers program so that the user can interactively create NumberThread s and assign them a priority. Modify the NumberThread s 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?
    11. 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.
    12. Modify the simulation in the previous 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.
    13. Modify your solution to the previous problem by having the balls bounce off the wall at a random angle.
    14. 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 of which 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 shouldn’t read a value from the text field until there’s a value to be read. The writer thread shouldn’t write a value to the text field until the reader has read the previous value.
    15. 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.
    16. Challenge: Create a slide show program, which repeatedly cycles through an array of images. The action of displaying the images should be a separate thread. The frame thread should handle the user interface. Give the user some controls that let it pause, stop, start, speed up, and slow down the images.
    17. 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, which we introduced in Chapter {chapter-inheritance}.
    18. 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.
    You have attempted of activities on this page.