Java Reference
In-Depth Information
• Java programs can have multiple threads of execution, where each thread has its own method-
call stack and program counter, allowing it to execute concurrently with other threads. This ca-
pability is called multithreading.
• In a multithreaded application, threads can be distributed across multiple processors (if available)
so that multiple tasks execute in parallel and the application can operate more efficiently.
• The JVM creates threads to run a program and for housekeeping tasks such as garbage collection.
• Multithreading can also increase performance on single-processor systems—when one thread
cannot proceed (because, for example, it's waiting for the result of an I/O operation), another
can use the processor.
• The vast majority of programmers should use existing collection classes and interfaces from the
concurrency APIs that manage synchronization for you.
Section 23.2 Thread States and Life Cycle
• A new thread begins its life cycle in the
new
state (p. 961). When the program starts the thread, it's
placed in the
runnable
state. A thread in the
runnable
state is considered to be executing its task.
•A
runnable
thread transitions to the
waiting
state (p. 961) to wait for another thread to perform a
task. A
waiting
thread transitions to
runnable
when another thread notifies it to continue executing.
•A
runnable
thread can enter the
timed waiting
state (p. 961) for a specified interval of time, transi-
tioning back to
runnable
when that time interval expires or when the event it's waiting for occurs.
•A
runnable
thread can transition to the
timed waiting
state if it provides an optional wait interval
when it's waiting for another thread to perform a task. Such a thread will return to the
runnable
state when it's notified by another thread or when the timed interval expires.
• A sleeping thread (p. 961) remains in the
timed waiting
state for a designated period of time, after
which it returns to the
runnable
state.
•A
runnable
thread transitions to the
blocked
state (p. 961) when it attempts to perform a task that
cannot be completed immediately and the thread must temporarily wait until that task completes.
At that point, the
blocked
thread transitions to the
runnable
state, so it can resume execution.
•A
runnable
thread enters the
terminated
state (p. 961) when it successfully completes its task or
otherwise terminates (perhaps due to an error).
• At the operating-system level, the
runnable
state (p. 962) encompasses two separate states. When a
thread first transitions to the
runnable
state from the
new
state, it's in the
ready
state (p. 962). A
ready
thread enters the
running
state (p. 962) when the operating system dispatches it.
• Most operating systems allot a quantum (p. 962) in which a thread performs its task. When this
expires, the thread returns to the
ready
state and another thread is assigned to the processor.
• Thread scheduling determines which thread to dispatch based on thread priorities.
• The job of an operating system's thread scheduler (p. 962) is to determine which thread runs next.
• When a higher-priority thread enters the
ready
state, the operating system generally preempts the
currently
running
thread (an operation known as preemptive scheduling; p. 963).
• Depending on the operating system, higher-priority threads could postpone—possibly indefi-
nitely (p. 963)—the execution of lower-priority threads.
Section 23.3 Creating and Executing Threads with the
Executor
Framework
•A
Runnable
(p. 963) object represents a task that can execute concurrently with other tasks.
• Interface
Runnable
declares method
run
(p. 963) in which you place the code that defines the
task to perform. The thread executing a
Runnable
calls method
run
to perform the task.
• A program will not terminate until its last thread completes execution.