|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Thread
public class Thread
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();
| Field Summary | |
|---|---|
static int |
MAX_PRIORITY
|
static int |
MIN_PRIORITY
The minimum/default/maximum priority that a thread can have. |
static int |
NORM_PRIORITY
|
| Constructor Summary | |
|---|---|
Thread()
Allocates a new Thread object. |
|
Thread(Runnable target)
Allocates a new Thread object with a specific target object whose run method is called. |
|
Thread(Runnable target,
String name)
Allocates a new Thread object with the given target and name. |
|
Thread(String name)
Allocates a new Thread object with the given name. |
|
| Method Summary | |
|---|---|
static int |
activeCount()
Returns the current number of active threads in the virtual machine. |
static Thread |
currentThread()
Returns a reference to the currently executing Thread object. |
String |
getName()
Returns this thread's name. |
int |
getPriority()
Returns this thread's priority. |
void |
interrupt()
Interrupts this thread. |
boolean |
isAlive()
Tests if this thread is alive. |
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 |
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. |
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 the thread's name and priority. |
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 |
|---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int MIN_PRIORITY
public static final int NORM_PRIORITY
public static final int MAX_PRIORITY
| Constructor Detail |
|---|
public Thread()
public Thread(Runnable target)
public Thread(Runnable target,
String name)
public Thread(String name)
| Method Detail |
|---|
public static int activeCount()
public static Thread currentThread()
public final String getName()
public final int getPriority()
public final void interrupt()
public final void join()
throws InterruptedException
InterruptedExceptionpublic final boolean isAlive()
public void run()
run in interface Runnablepublic final void setPriority(int newPriority)
newPriority - priority to set this thread to
IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
public static void sleep(long millis)
throws InterruptedException
InterruptedExceptionpublic void start()
public String toString()
toString in class Objectpublic static void yield()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||