The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Essential Java Classes
Lesson: Doing Two or More Tasks At Once: Threads

## Summary

> 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. The `Thread` class defines and implements Java threads. You can subclass the `Thread` class to provide your own thread implementations or you can use the `Runnable` interface.
> `java.lang.Runnable`
>   The Java language library also defines the `Runnable` interface, which allows any class to provide the body (the `run` 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`, and `notifyAll`.
> `java.lang.ThreadGroup`
>   All threads belong to a thread group, which typically contains related threads. The `ThreadGroup` class in the `java.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 catch `ThreadDeath` 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) 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 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.
> Concurrent Programming in Java, Second Edition
>   For further information about threads programming, see Doug Lea's highly acclaimed book written for intermediate and advanced threads programmers. The second edition is available from amazon.com.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form