java.lang
Class Thread

java.lang.Object
  extended by java.lang.Thread

public class Thread
extends Object

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

Since:
JDK1.0, CLDC 1.0
Version:
1/1/08 (CLDC 1.1)
See Also:
Runnable, run()

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 priority that is assigned to the primordial thread.
 
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)
           
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.
 void destroy()
          destroy destroys the thread without any cleanup.
 Object getGroupNext()
           
 String getName()
          Return the name of this thread.
 int getPriority()
          Returns this thread's priority.
 void interrupt()
          Set the interrupted flag.
static boolean interrupted()
           
 boolean isAlive()
          Tests if this thread is alive.
 boolean isDaemon()
          Set the daemon flag.
 boolean isInterrupted()
           
 void join()
          Waits for this thread to die.
 void join(int timeout)
          Waits at most millis milliseconds 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)
           
 void setPriority(int priority)
          Set the priority of this thread.
static void sleep(long aMilliseconds)
          Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
 void start_native()
           
 void start()
          Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
 void stop()
          stop destroys the thread while attempting some cleanup.
 String toString()
          Return a string representation of this Thread, including its 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

MIN_PRIORITY

public static final int MIN_PRIORITY
The minimum priority that a thread can have. The value is 1.

See Also:
Constant Field Values

NORM_PRIORITY

public static final int NORM_PRIORITY
The priority that is assigned to the primordial thread. The value is 5.

See Also:
Constant Field Values

MAX_PRIORITY

public static final int MAX_PRIORITY
The maximum priority that a thread can have. The value is 10.

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.

See Also:
Runnable

Thread

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.

Parameters:
name - the name of the new thread.

Thread

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.

Thread

public Thread(Runnable target,
              String name)
Method Detail

isAlive

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.

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.

See Also:
start(), Runnable.run()

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

Throws:
IllegalThreadStateException - if the thread was already started.
See Also:
run()

start_native

public final void start_native()

currentThread

public static Thread currentThread()
Returns a reference to the currently executing Thread object.

Returns:
the currently executing thread.

yield

public static void yield()
Causes the currently executing thread object to temporarily pause and allow other threads to execute.


sleep

public static void sleep(long aMilliseconds)
                  throws InterruptedException
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:
aMilliseconds - 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:
Object.notify()

getPriority

public final int getPriority()
Returns this thread's priority.

Returns:
this thread's priority.
See Also:
setPriority(int)

setPriority

public final void setPriority(int priority)
Set the priority of this thread. Higher number have higher priority. The scheduler will always run the highest priority thread in preference to any others. If more than one thread of that priority exists the scheduler will time-slice them. In order for lower priority threas to run a higher priority thread must cease to be runnable. i.e. it must exit, sleep or wait on a monitor. It is not sufficient to just yield.

Threads inherit the priority of their parent. The primordial thread has priority NORM_PRIORITY.

Parameters:
priority - must be between MIN_PRIORITY and MAX_PRIORITY.

interrupt

public void interrupt()
Set the interrupted flag. If we are asleep we will wake up and an InterruptedException will be thrown.

Since:
JDK 1.0, CLDC 1.1

interrupted

public static boolean interrupted()

isInterrupted

public final boolean isInterrupted()

destroy

public final void destroy()
destroy destroys the thread without any cleanup. Any monitors held by the thread remain locked, no attempt to make nice with the underlying os is even attempted. Destroy should only be used in an emergency, and probably only by isolates.


stop

public final void stop()
stop destroys the thread while attempting some cleanup. The thread is marked as dying, a thread death exception is thrown, and propagated up the stack. The monitors held by the thread are unlocked. Note that using the stop() method, will not destroy the thread immediately -- the cleanup is peformed when the dying thread is re-scheduled.


isDaemon

public final boolean isDaemon()
Set the daemon flag. If a thread is a daemon thread its existence will not prevent a JVM from exiting.


setDaemon

public final void setDaemon(boolean on)

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.

join

public final void join(int timeout)
                throws InterruptedException
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.

Parameters:
timeout - the length of time to wait 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.

getGroupNext

public final Object getGroupNext()

activeCount

public static int activeCount()
Returns the current number of active threads in the virtual machine.

Returns:
the current number of active threads.

getName

public String getName()
Return the name of this thread.


toString

public String toString()
Return a string representation of this Thread, including its name and priority.

Overrides:
toString in class Object
Returns:
a string representation of the object.


Copyright © 2007 Sentilla Corporation. All Rights Reserved.