Overview | Package | Class | Tree | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## java.lang
Class Thread

    java.lang.Object
      java.lang.Thread

All Implemented Interfaces:
  Runnable

    public class Thread

extends Object
implements Runnable

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new `Thread` object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named `main` of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

- All threads that are not daemon threads have died, either by returning from the call to the `run` method or by throwing an exception that propagates beyond the `run` method.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of `Thread`. This subclass should override the `run` method of class `Thread`. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:

>          class PrimeThread extends Thread {
>              int minPrime;
>              PrimeThread(int minPrime) {
>                  this.minPrime = minPrime;
>              }

>              public void run() {
>                  // compute primes larger than minPrime
>                   . . .
>              }
>          }

The following code would then create a thread and start it running:

>          PrimeThread p = new PrimeThread(143);
>          p.start();

The other way to create a thread is to declare a class that implements the `Runnable` interface. That class then implements the `run` method. An instance of the class can then be allocated, passed as an argument when creating `Thread`, and started. The same example in this other style looks like the following:

>          class PrimeRun implements Runnable {
>              int minPrime;
>              PrimeRun(int minPrime) {
>                  this.minPrime = minPrime;
>              }

>              public void run() {
>                  // compute primes larger than minPrime
>                   . . .
>              }
>          }

The following code would then create a thread and start it running:

>          PrimeRun p = new PrimeRun(143);
>          new Thread(p).start();

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.

Since:
  JDK1.0
Version:
  1.106, 02/02/00
Author:
  unascribed, Helge Böhme (JControl)
See Also:
  `Runnable`, `run()`
Available on JControl Devices:

  all

Field Summary
`static int` | `MAX_PRIORITY` The maximum priority that a thread can have.
`static int` | `MIN_PRIORITY` The minimum priority that a thread can have.
`static int` | `NORM_PRIORITY` The default priority that is assigned to a thread.

Constructor Summary
`Thread()` Allocates a new `Thread` object.
`Thread(Runnable target)` Allocates a new `Thread` object.

Method Summary
`static Thread` | `currentThread()` Returns a reference to the currently executing thread object.
`int` | `getPriority()` Returns this thread's priority.
`void` | `interrupt()` Interrupts this thread.
`static boolean` | `interrupted()` Tests whether the current thread has been interrupted.
`boolean` | `isAlive()` Tests if this thread is alive.
`boolean` | `isDaemon()` Tests if this thread is a daemon thread.
`boolean` | `isInterrupted()` Tests whether this thread has been interrupted.
`void` | `join()` Waits for this thread to die.
`void` | `run()` If this thread was constructed using a separate `Runnable` run object, then that `Runnable` object's `run` method is called; otherwise, this method does nothing and returns.
`void` | `setDaemon(boolean on)` Marks this thread as either a daemon thread or a user thread.
`void` | `setPriority(int newPriority)` Changes the priority of this thread.
`void` | `start()` Causes this thread to begin execution; the Java Virtual Machine calls the `run` method of this thread.
`static void` | `yield()` Causes the currently executing thread object to temporarily pause and allow other threads to execute.

Methods inherited from class java.lang.Object
`clone, equals, notifyAll, wait`

Field Detail

### MAX_PRIORITY

    public static final int MAX_PRIORITY

  The maximum priority that a thread can have.

  See Also:
    Constant Field Values

### MIN_PRIORITY

    public static final int MIN_PRIORITY

  The minimum priority that a thread can have.

  See Also:
    Constant Field Values

### NORM_PRIORITY

    public static final int NORM_PRIORITY

  The default priority that is assigned to a thread.

  See Also:
    Constant Field Values

Constructor Detail

### Thread

    public Thread()

  Allocates a new `Thread` object.

  Threads created this way must have overridden their `run()` method to actually do anything. An example illustrating this method being used follows:

  >          import java.lang.*;

  >          class plain01 implements Runnable {
  >              String name;
  >              plain01() {
  >                  name = null;
  >              }
  >              plain01(String s) {
  >                  name = s;
  >              }
  >              public void run() {
  >                  if (name == null)
  >                      System.out.println("A new thread created");
  >                  else
  >                      System.out.println("A new thread with name " + name +
  >                                         " created");
  >              }
  >          }
  >          class threadtest01 {
  >              public static void main(String args[] ) {
  >                  int failed = 0 ;

  >                  Thread t1 = new Thread();
  >                  if (t1 != null)
  >                      System.out.println("new Thread() succeed");
  >                  else {
  >                      System.out.println("new Thread() failed");
  >                      failed++;
  >                  }
  >              }
  >          }

### Thread

    public Thread(Runnable target)

  Allocates a new `Thread` object.

  Parameters:
    `target` - the object whose `run` method is called.

Method Detail

### currentThread

    public static Thread currentThread()

  Returns a reference to the currently executing thread object.

  Returns:
    the currently executing thread.

### getPriority

    public final int getPriority()

  Returns this thread's priority.

  Returns:
    this thread's name.
  See Also:
    `setPriority(int)`, `setPriority(int)`

### interrupt

    public void interrupt()

  Interrupts this thread.

  If this thread is blocked in an invocation of the `wait()` method of the `Object` class, or of the `sleep(int)` method of the `ThreadExt` class, then its interrupt status will be cleared and it will receive an `InterruptedException`.

  If none of the previous conditions hold then this thread's interrupt status will be set.

### interrupted

    public static boolean interrupted()

  Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).

  Returns:
    `true` if the current thread has been interrupted; `false` otherwise.
  See Also:
    `isInterrupted()`

### isAlive

    public boolean isAlive()

  Tests if this thread is alive. A thread is alive if it has been started and has not yet died.

  Returns:
    `true` if this thread is alive; `false` otherwise.

### isDaemon

    public boolean isDaemon()

  Tests if this thread is a daemon thread.

  Returns:
    `true` if this thread is a daemon thread; `false` otherwise.
  See Also:
    `setDaemon(boolean)`

### isInterrupted

    public boolean isInterrupted()

  Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.

  Returns:
    `true` if this thread has been interrupted; `false` otherwise.
  See Also:
    `interrupted()`

### join

    public final void join()
                    throws InterruptedException

  Waits for this thread to die.

  Throws:
    `InterruptedException` - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

### run

    public void run()

  If this thread was constructed using a separate `Runnable` run object, then that `Runnable` object's `run` method is called; otherwise, this method does nothing and returns.

  Subclasses of `Thread` should override this method. java.lang.Runnable, java.lang.String)

  Specified by:
    `run` in interface `Runnable`
  See Also:
    `Runnable.run()`

### setDaemon

    public void setDaemon(boolean on)

  Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

  Parameters:
    `on` - if `true`, marks this thread as a daemon thread.
  See Also:
    `isDaemon()`

### setPriority

    public void setPriority(int newPriority)

  Changes the priority of this thread.

  Parameters:
    `newPriority` - priority to set this thread to
  Throws:
    `IllegalArgumentException` - If the priority is not in the range `MIN_PRIORITY` to `MAX_PRIORITY`.
  See Also:
    `getPriority()`, `getPriority()`, `MAX_PRIORITY`, `MIN_PRIORITY`

### start

    public void start()

  Causes this thread to begin execution; the Java Virtual Machine calls the `run` method of this thread.

  The result is that two threads are running concurrently: the current thread (which returns from the call to the `start` method) and the other thread (which executes its `run` method).

  See Also:
    `run()`

### yield

    public static void yield()

  Causes the currently executing thread object to temporarily pause and allow other threads to execute.

Overview | Package | Class | Tree | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD