|Threads of Control|
This lesson has provided a great deal of information about using threads in the Java development environment. Threads are supported by various components of the Java development environment, and it can be hard to find the features that you need. This section summarizes where in the Java environment you can find various classes, methods, and language features that participate in the Java threads story.
Package Support of Threads
- In the Java development enviroment, threads are objects that derive from java.lang's
Threadclass defines and implements Java threads. You can subclass the
Threadclass to provide your own thread implementations or you can use the
- The Java language library also defines the
Runnableinterface, which allows any class to provide the body (the
runmethod) for a thread.
- The root class,
Object, defines three methods you can use to synchronize methods around a condition variable:
- All threads belong to a thread group, which typically contains related threads. The
ThreadGroupclass in the
java.langpackage implements groups of threads.
- A thread is normally killed by throwing a
ThreadDeathobject at it. Rarely, a thread might need to catch
ThreadDeathto clean up before it dies.
Language Support of ThreadsThe Java language has two keywords related to the synchronization of threads:
volatile(which is not implemented in JDK 1.0) and
synchronized. Both of these language features help ensure the integrity of data that is shared between two concurrently running threads. Multithreaded Programs discusses thread synchronization issues.
Runtime Support of ThreadsThe Java runtime system contains the scheduler, which is responsible for running all the existing threads. The Java scheduler uses a fixed priority scheduling algorithm which boils down to this simple rule of thumb:
Rule of thumb: At any given time, the highest priority thread is running. However, this is not guaranteed. The thread scheduler may choose to run a lower priority thread to avoid starvation. For this reason, use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.
Other Thread Information
- Threads in Applets
- When you write applets that use threads, you may have to make special provisions, such as ensuring that your applet is well-behaved. Also, some browsers impose security restrictions for applets based on which thread group a thread is in.
|Threads of Control|