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
java.lang.Thread
- In the Java development enviroment, threads are objects that derive from java.lang's
Thread
class. TheThread
class defines and implements Java threads. You can subclass theThread
class to provide your own thread implementations or you can use theRunnable
interface.java.lang.Runnable
- The Java language library also defines the
Runnable
interface, which allows any class to provide the body (therun
method) for a thread.java.lang.Object
- The root class,
Object
, defines three methods you can use to synchronize methods around a condition variable:wait
,notify
, andnotifyAll
.java.lang.ThreadGroup
- All threads belong to a thread group, which typically contains related threads. The
ThreadGroup
class in thejava.lang
package implements groups of threads.java.lang.ThreadDeath
- A thread is normally killed by throwing a
ThreadDeath
object at it. Rarely, a thread might need to catchThreadDeath
to clean up before it dies.
Language Support of Threads
The Java language has two keywords related to the synchronization of threads:volatile
(which is not implemented in JDK 1.0) andsynchronized
. 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 Threads
The 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.
No comments:
Post a Comment