public class

Thread

extends Object
implements Runnable
java.lang.Object
   ↳ java.lang.Thread

Class Overview

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.

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 {
 	long minPrime;
 	
 	PrimeThread(long 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 {
 	long minPrime;
 	
 	PrimeRun(long 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();
 

Summary

Constants
int MAX_PRIORITY The maximum priority that a thread can have.
int MIN_PRIORITY The minimum priority that a thread can have.
int NORM_PRIORITY The default priority that is assigned to a thread.
Public Constructors
Thread()
Allocates a new Thread object.
Thread(Runnable target)
Allocates a new Thread object with a specific target object whose run method is called.
Public Methods
static int activeCount()
Returns the current number of active threads in the VM.
static Thread currentThread()
Returns a reference to the currently executing thread object.
final int getPriority()
Returns this thread's priority.
void interrupt()
Interrupts this thread.
final boolean isAlive()
Tests if this thread is alive.
synchronized final 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.
final void setPriority(int newPriority)
Changes the priority of this thread.
static void sleep(long millis)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
synchronized void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
String toString()
Returns a string representation of this thread, including a unique number that identifies the thread and the thread's priority.
static void yield()
Causes the currently executing thread object to temporarily pause and allow other threads to execute.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Runnable

Constants

public static final int MAX_PRIORITY

The maximum priority that a thread can have.

Constant Value: 10 (0x0000000a)

public static final int MIN_PRIORITY

The minimum priority that a thread can have.

Constant Value: 1 (0x00000001)

public static final int NORM_PRIORITY

The default priority that is assigned to a thread.

Constant Value: 5 (0x00000005)

Public Constructors

public Thread ()

Allocates a new Thread object.

Threads created this way must have overridden their run() method to actually do anything.

See Also

public Thread (Runnable target)

Allocates a new Thread object with a specific target object whose run method is called.

Parameters
target the object whose run method is called.

Public Methods

public static int activeCount ()

Returns the current number of active threads in the VM.

Returns
  • the current number of active threads.

public static Thread currentThread ()

Returns a reference to the currently executing thread object.

Returns
  • the currently executing thread.

public final int getPriority ()

Returns this thread's priority.

Returns
  • this thread's priority.

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 join(), sleep(long) methods of this 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.

Interrupting a thread that is not alive need not have any effect.

public final 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.

public final synchronized void join ()

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.

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.

See Also

public final 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.

public static void sleep (long millis)

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.

Parameters
millis the length of time to sleep in milliseconds.
Throws
InterruptedException if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
See Also

public synchronized 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).

Throws
IllegalThreadStateException if the thread was already started.
See Also

public String toString ()

Returns a string representation of this thread, including a unique number that identifies the thread and the thread's priority.

Returns
  • a string representation of this thread.

public static void yield ()

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