Java Deitel

Páginas: 11 (2681 palabras) Publicado: 1 de octubre de 2012
23
Multithreading
The most general definition of beauty…Multeity in Unity.
—Samuel Taylor Coleridge

Do not block the way of inquiry.
—Charles Sanders Peirce

OBJECTIVES
In this chapter you will learn:
■ ■ ■ ■ ■ ■ ■

What threads are and why they are useful. How threads enable you to manage concurrent activities. The life cycle of a thread. Thread priorities and scheduling. To createand execute Runnables. Thread synchronization. What producer/consumer relationships are and how they are implemented with multithreading. To enable multiple threads to update Swing GUI components in a thread-safe manner. About interfaces Callable and Future, which you can use with threading to execute tasks that return results.

A person with one watch knows what time it is; a person with twowatches is never sure.
—Proverb

Learn to labor and to wait.
—Henry Wadsworth Longfellow

The world is moving so fast these days that the man who says it can’t be done is generally interrupted by someone doing it.
—Elbert Hubbard





© 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

2

Chapter 23 Multithreading

Student Solution Exercises
23.3 Statewhether each of the following is true or false. If false, explain why. a) Method sleep does not consume processor time while a thread sleeps. ANS: True. b) Declaring a method synchronized guarantees that deadlock cannot occur. ANS: False. Deadlocks can occur if the lock on an object is never released. c) Once a Lock has been obtained by a thread, the Lock object will not allow another thread toobtain the lock until the first thread releases it. ANS: True. d) Swing components are thread safe. ANS: False. Swing components are not thread safe. All interactions with Swing GUI components should be performed in the event-dispatching thread. Define each of the following terms. a) thread ANS: An individual execution context of a program. b) multithreading ANS: The ability of more than one threadto execute concurrently. c) runnable state ANS: A state in which the thread is capable of running (if the processor becomes available). d) timed waiting state ANS: A state in which the thread cannot use the processor because it is waiting for a time interval to expire. e) preemptive scheduling ANS: A thread of higher priority enters a running state and is assigned to the processor. The threadpreempted from the processor is placed back in the ready state according to its priority. f) Runnable interface ANS: An interface that provides a run method. By implementing the Runnable interface, any class can be executed as a separate thread. g) notifyAll method ANS: Transitions all threads waiting on an object’s monitor to the runnable state. h) producer/consumer relationship ANS: A relationshipin which a producer and a consumer share common data. The producer typically wants to "produce" (add information) and the consumer wants to "consume" (remove information). i) quantum ANS: A small amount of processor time, also called a time slice.

23.4

23.7 Two problems that can occur in systems that allow threads to wait are deadlock, in which one or more threads will wait forever for anevent 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 multithreaded Java programs. ANS: Deadlock: If we have two threads named thread1 and thread2, deadlock might occur in the following situation: If thread1 is waiting for thread2 to complete a task, and thread2is waiting for thread1 to complete a task, then neither thread can continue. Since both threads are in the waiting state, neither thread can be signaled to continue executing. Indefinite Postponement: This typically occurs because threads of higher priority are scheduled before threads of lower priority. One way to prevent this is for the sched-

© 2007 Pearson Education, Inc., Upper Saddle...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Deitel
  • Como Programar C C++ y Java 4ta Edición
  • Ejercicios Deitel
  • Java
  • java
  • javo
  • Javo
  • Javiera

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS