Threads are sub-processes that run in parallel to the main program.
On a single processor, of course, the parallelism is only apparent
from the high speed switching of programs in and out of the system.
Yet, even in that case threading can provide significant benefits.
For example, perhaps one thread takes care of slow I/O tasks such
as downloading a file over the internet. Then while that process
is waiting for data, other threads can do useful work.
Java makes the creation and running of a thread quite easy. We
will concentrate on the basics of threading in this chapter. However,
subtleties and complications arise when using multiple threads must
interact and access and modify common resources. Avoiding data
race and other interference problems among the threads becomes
very important.
The sections here cover the following topics:
- Introduction to threads - a brief
overview of threads and the two ways to create threads.
- Stopping Threads - stopping a
thread requires killing the thread process entirely by making
it return from its run()
method.
- Multi-Processing - more about topics
related using multiple threads.
- Thread Tasks - more about where
threads are useful.
- Animations - threads are often
used to create animated graphics.
- We discuss four basic types of multiple thread programs and
include a demo for each:
- Non-interacting - threads
run with no interaction among each other.
- Task Splitting - several
threads attack different parts of the same problem.
- Exclusivity - multiple
threads attempt to access the same data. Synchronization techniques
must be applied to avoid interference problems such as data
race conditions.
- Communicating - multiple
threads access each other and also must synchronize to avoid
interference problems.
- Priority/Scheduling issues
of threads.
- More about threads - brief
discussions of some additional thread topics.
- Exercises
Latest update: Nov. 8, 2004
|