Answers to Self Review Exercises


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.


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.



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.

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

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

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.


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?


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


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.


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.


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.


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.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

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