Java Reference
In-Depth Information
CHAPTER
15
Threads
Threads allow you to perform multiple tasks at the same time. In this chapter,
I will discuss how threads are created in Java and how they behave after they
start running. Creating threads has advantages, but using multiple threads
creates data integrity issues, so I will need to discuss the various synchroniza-
tion issues that arise. Topics discussed include the Runnable interface; the
Thread, Timer, and TimerTask classes; the synchronization keyword; and
avoiding deadlock.
Overview of Threads
A thread is defined as a path of execution, a collection of statements that exe-
cute in a specific order. The programs we have written up until now in the
course have had a single path of execution: the main() method. When a Java
application is executed, the main() method runs in its own thread. Within the
path of execution of main(), you can start new threads to perform different
tasks.
From a programming point of view, creating multiple threads is equivalent
to being able to invoke multiple methods at the same time. You can have a
503
Search WWH ::




Custom Search