26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

888 Multithreading Chapter 15<br />

• A running thread enters the blocked state when the thread issues an input/output request. A<br />

blocked thread becomes ready when the I/O it is waiting for completes. A blocked thread cannot<br />

use a processor even if one is available.<br />

• When a running method calls wait, the thread enters a waiting state for the particular object in<br />

which the thread was running. A thread in the waiting state for a particular object becomes ready<br />

on a call <strong>to</strong> notify issued by another thread associated with that object.<br />

• Every thread in the waiting state for a given object becomes ready on a call <strong>to</strong> notifyAll by<br />

another thread associated with that object.<br />

• Every <strong>Java</strong> thread has a priority in the range Thread.MIN_PRIORITY (a constant of 1) and<br />

Thread.MAX_PRIORITY (a constant of 10). By default, each thread is given priority<br />

Thread.NORM_PRIORITY (a constant of 5).<br />

• Some <strong>Java</strong> platforms support a concept called timeslicing and some do not. Without timeslicing,<br />

threads of equal priority run <strong>to</strong> completion before their peers get a chance <strong>to</strong> execute. With<br />

timeslicing, each thread receives a brief burst of processor time called a quantum during which<br />

that thread can execute. At the completion of the quantum, even if that thread has not finished<br />

executing, the processor is taken away from that thread and given <strong>to</strong> the next thread of equal priority,<br />

if one is available.<br />

• The job of the <strong>Java</strong> scheduler is <strong>to</strong> keep a highest priority thread running at all times and, if<br />

timeslicing is available, <strong>to</strong> ensure that several equally high-priority threads each execute for a<br />

quantum in round-robin fashion.<br />

• A thread’s priority can be adjusted with the setPriority method. Method getPriority<br />

returns the thread’s priority.<br />

• A thread can call the yield method <strong>to</strong> give other threads a chance <strong>to</strong> execute.<br />

• Every object that has synchronized methods has a moni<strong>to</strong>r. The moni<strong>to</strong>r lets only one thread<br />

at a time execute a synchronized method on the object.<br />

• A thread executing in a synchronized method may determine that it cannot proceed, so the<br />

thread voluntarily calls wait. This removes the thread from contention for the processor and<br />

from contention for the object.<br />

• A thread that has called wait is awakened by a thread that calls notify. The notify acts as<br />

a signal <strong>to</strong> the waiting thread that the condition the waiting thread has been waiting for is now (or<br />

could be) satisfied, so it is acceptable for that thread <strong>to</strong> reenter the moni<strong>to</strong>r.<br />

• A daemon thread serves other threads. When only daemon threads remain in a program, <strong>Java</strong> will<br />

exit. If a thread is <strong>to</strong> be a daemon, it must be set as such before its start method is called.<br />

• To support multithreading in a class derived from some class other than Thread, implement the<br />

Runnable interface in that class.<br />

• Implementing the Runnable interface gives us the ability <strong>to</strong> treat the new class as a Runnable<br />

object (just like inheriting from a class allows us <strong>to</strong> treat our subclass as an object of its superclass).<br />

As with deriving from the Thread class, the code that controls the thread is placed in the<br />

run method.<br />

• A thread with a Runnable class is created by passing <strong>to</strong> the Thread class construc<strong>to</strong>r a reference<br />

<strong>to</strong> an object of the class that implements the Runnable interface. The Thread construc<strong>to</strong>r<br />

registers the run method of the Runnable object as the method <strong>to</strong> be invoked when the thread<br />

begins execution.<br />

• Class ThreadGroup contains the methods for creating and manipulating groups of related<br />

threads in a program.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!