FileDocCategorySizeDatePackage
Thread.javaAPI DocJ2ME CLDC 1.110085Wed Feb 05 15:56:00 GMT 2003java.lang

Thread

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.

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();

author
unascribed
version
12/17/01 (CLDC 1.1)
see
java.lang.Runnable
see
java.lang.Runtime#exit(int)
see
java.lang.Thread#run()
since
JDK1.0, CLDC 1.0

Fields Summary
private int
priority
private Thread
threadQ
private Runnable
target
private char[]
name
public static final int
MIN_PRIORITY
The minimum priority that a thread can have.
public static final int
NORM_PRIORITY
The default priority that is assigned to a thread.
public static final int
MAX_PRIORITY
The maximum priority that a thread can have.
private static int
threadInitNumber
Constructors Summary
public Thread()
Allocates a new Thread object.

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

see
java.lang.Runnable

        init(null, "Thread-" + nextThreadNum());
    
public Thread(String name)
Allocates a new Thread object with the given name. Threads created this way must have overridden their run() method to actually do anything.

param
name the name of the new thread.

        init(null, name);
    
public Thread(Runnable target)
Allocates a new Thread object with a specific target object whose run method is called.

param
target the object whose run method is called.

        init(target, "Thread-" + nextThreadNum());
    
public Thread(Runnable target, String name)
Allocates a new Thread object with the given target and name.

param
target the object whose run method is called.
param
name the name of the new thread.

        init(target, name);
    
Methods Summary
public static native intactiveCount()
Returns the current number of active threads in the virtual machine.

return
the current number of active threads.

public static native java.lang.ThreadcurrentThread()
Returns a reference to the currently executing Thread object.

return
the currently executing thread.

public final java.lang.StringgetName()
Returns this thread's name. Note that in CLDC the name of the thread can only be set when creating the thread.

return
this thread's name.

        return String.valueOf(name);
    
public final intgetPriority()
Returns this thread's priority.

return
this thread's priority.
see
java.lang.Thread#setPriority(int)

        return priority;
    
private voidinit(java.lang.Runnable target, java.lang.String name)
Initialize a Thread.

param
target the object whose run() method gets called
param
name the name of the new thread

        Thread parent = currentThread();
        this.target = target;
        this.name  = name.toCharArray();
        this.priority = parent.getPriority();
        setPriority0(priority);
    
public voidinterrupt()
Interrupts this thread. In an implementation conforming to the CLDC Specification, this operation is not required to cancel or clean up any pending I/O operations that the thread may be waiting for.

since
JDK 1.0, CLDC 1.1

        interrupt0();
    
private native voidinterrupt0()

public final native booleanisAlive()
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.

return
true if this thread is alive; false otherwise.

public final synchronized voidjoin()
Waits for this thread to die.

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

        while (isAlive()) {
            wait(1000);
        }
    
private static synchronized intnextThreadNum()

         
        return ++threadInitNumber;
    
public voidrun()
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
java.lang.Thread#start()
see
java.lang.Runnable#run()

        if (target != null) {
            target.run();
        }
    
public final voidsetPriority(int newPriority)
Changes the priority of this thread.

param
newPriority priority to set this thread to
exception
IllegalArgumentException If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
see
java.lang.Thread#getPriority()
see
java.lang.Thread#MAX_PRIORITY
see
java.lang.Thread#MIN_PRIORITY

        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        setPriority0(priority = newPriority);
    
private native voidsetPriority0(int newPriority)

public static native voidsleep(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.

param
millis the length of time to sleep in milliseconds.
exception
InterruptedException if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
see
java.lang.Object#notify()

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

exception
IllegalThreadStateException if the thread was already started.
see
java.lang.Thread#run()

public java.lang.StringtoString()
Returns a string representation of this thread, including the thread's name and priority.

return
a string representation of this thread.

        return "Thread[" + getName() + "," + getPriority() + "]";
    
public static native voidyield()
Causes the currently executing thread object to temporarily pause and allow other threads to execute.