23.3 |
State whether each of the following is true or false. If false, explain why.
- Method sleep does not consume processor time while a thread sleeps.
- Using a Lock guarantees that deadlock cannot occur.
- 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.
- Swing components are thread safe.
|
23.4 |
Define each of the following terms.
- thread
- multithreading
- runnable state
- timed waiting state
- preemptive scheduling
- Runnable interface
- signal method of class Condition
- producer/consumer relationship
- quantum
|
23.5 |
Discuss each of the following terms in the context of Java's threading mechanisms:
- Lock
- producer
- consumer
- await
- signal
- 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.
|