Wrap-Up

Answers to Self Review Exercises

23.1

a) single, multi. b) its run method ends. c) THRead.sleep. d) signal. e) signalAll. f) runnable, terminated. g) timed waiting. h) ready, running. i) Executor. j) shutdown. k) await, waiting. l) producer/consumer, producer, consumer. m) circular buffer. n) ArrayBlockingQueue. o) synchronized.

23.2

a) True. b) True. c) False. Timeslicing allows a thread to execute until its timeslice (or quantum) expires. Then other threads of equal priority can execute. d) False. When a thread's quantum expires, the thread returns to the ready state and the operating system assigns to the processor another thread. e) True.

Exercises

23.3

State whether each of the following is true or false. If false, explain why.

  1. Method sleep does not consume processor time while a thread sleeps.
  2. Using a Lock guarantees that deadlock cannot occur.
  3. Once a Lock has been obtained by a thread, the Lock object will not allow another thread to obtain the lock until the first thread releases it.
  4. Swing components are thread safe.

23.4

Define each of the following terms.

  1. thread
  2. multithreading
  3. runnable state
  4. timed waiting state
  5. preemptive scheduling
  6. Runnable interface
  7. signal method of class Condition
  8. producer/consumer relationship
  9. quantum

23.5

Discuss each of the following terms in the context of Java's threading mechanisms:

  1. Lock
  2. producer
  3. consumer
  4. await
  5. signal
  6. Condition

23.6

Two problems that can occur in systems like Java, that allow threads to wait, are deadlock, in which one or more threads will wait forever for an event that cannot occur, and indefinite postponement, in which one or more threads will be delayed for some unpredictably long time. Give an example of how each of these problems can occur in a multithreaded Java program.

23.7
23.8

Discuss the difference between Condition method await with no arguments and Condition method await with a time-interval argument. In particular, what states do threads enter, and how can those threads return to the runnable state?

23.9

Name three threads that are created automatically by the Java virtual machine and discuss the purpose of each thread.

23.10

Write a program that bounces a blue ball inside a JPanel. The ball should begin moving with a mousePressed event. When the ball hits the edge of the JPanel, it should bounce off the edge and continue in the opposite direction. The ball should be updated using a Runnable.

23.11

Modify the program in Exercise 23.10 to add a new ball each time the user clicks the mouse. Provide for a minimum of 20 balls. Randomly choose the color for each new ball.

23.12

Modify the program in Exercise 23.11 to add shadows. As a ball moves, draw a solid black oval at the bottom of the JPanel. You may consider adding a 3-D effect by increasing or decreasing the size of each ball when it hits the edge of the JPanel.

23.13

Modify the program in Exercise 23.11 or Exercise 23.12 to bounce the balls off each other when they collide. A collision should occur between two balls when the distance between the centers of those two balls is less than the sum of the two balls' radii.

Категории