Download the JDK
Search the Tutorials
Hide the TOC
Trail: Essential Classes
Lesson: Concurrency
Section: Thread Objects
Concurrency
Processes and Threads
Thread Objects
Defining and Starting a Thread
Pausing Execution with Sleep
Interrupts
Joins
The SimpleThreads Example
Synchronization
Thread Interference
Memory Consistency Errors
Synchronized Methods
Intrinsic Locks and Synchronization
Atomic Access
Liveness
Deadlock
Starvation and Livelock
Guarded Blocks
Immutable Objects
A Synchronized Class Example
A Strategy for Defining Immutable Objects
High Level Concurrency Objects
Lock Objects
Executors
Executor Interfaces
Thread Pools
Concurrent Collections
Atomic Variables
For Further Reading
Questions and Exercises
Home Page > Essential Classes > Concurrency
« Previous • Trail • Next »
Defining and Starting a Thread

> An application that creates an instance of `Thread` must provide the code that will run in that thread. There are two ways to do this:
> - Provide a `Runnable` object. The `Runnable` interface defines a single method, `run`, meant to contain the code executed in the thread. The `Runnable` object is passed to the `Thread` constructor, as in the ``HelloRunnable`` example:

>   >     public class HelloRunnable implements Runnable {

>   >         public void run() {
>   >             System.out.println("Hello from a thread!");
>   >         }

>   >         public static void main(String args[]) {
>   >             (new Thread(new HelloRunnable())).start();
>   >         }

>   >     }

> - Subclass `Thread`. The `Thread` class itself implements `Runnable`, though its `run` method does nothing. An application can subclass `Thread`, providing its own implementation of `run`, as in the ``HelloThread`` example:

>   >     public class HelloThread extends Thread {

>   >         public void run() {
>   >             System.out.println("Hello from a thread!");
>   >         }

>   >         public static void main(String args[]) {
>   >             (new HelloThread()).start();
>   >         }

>   >     }

> Notice that both examples invoke `Thread.start` in order to start the new thread.

> Which of these idioms should you use? The first idiom, which employs a `Runnable` object, is more general, because the `Runnable` object can subclass a class other than `Thread`. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of `Thread`. This lesson focuses on the first approach, which separates the `Runnable` task from the `Thread` object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

> The `Thread` class defines a number of methods useful for thread management. These include `static` methods, which provide information about, or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and `Thread` object. We'll examine some of these methods in the following sections.

« Previous • Trail • Next »
Previous page: Thread Objects
Next page: Pausing Execution with Sleep