java::lang::Thread Class Reference

Inheritance diagram for java::lang::Thread:

Inheritance graph
java::lang::Objectjava::lang::Interfacejava::lang::Runnable
[legend]
Collaboration diagram for java::lang::Thread:

Collaboration graph
java::lang::Objectjava::lang::Interfacejava::lang::ObjectRefjava::lang::Runnablejava::lang::Comparable
[legend]

List of all members.


Detailed Description

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:

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:


<blockquote>
     class PrimeThread extends Thread {
         long minPrime;
         PrimeThread(long minPrime) {
             this.minPrime = minPrime;
         }

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

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

<blockquote>

     PrimeThread p = new PrimeThread(143);
     p.start();
 
</blockquote>

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:


<blockquote>
     class PrimeRun implements Runnable {
         long minPrime;
         PrimeRun(long minPrime) {
             this.minPrime = minPrime;
         }

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

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

<blockquote>

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

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.

Author:
Yuh-Jye Chang
Version:
1.106, 02/02/00
Since:
JDK1.0

Public Member Functions

 Thread (const Ref< Runnable > &target=0)
 Allocates a new Thread object.
 Thread (const Thread &src)
Threadoperator= (const Thread &)
virtual void start (const Ref< Runnable > &target)
 Causes this thread to begin execution on the assigned target.
virtual void start ()
 Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
virtual 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.
jboolean isAlive () const
 Tests if this thread is alive.
void setPriority (jint newPriority)
 Changes the priority of this thread.
jint getPriority () const
 Returns this thread's priority.
void join (jlong millis=0)
 Waits at most millis milliseconds for this thread to die.
virtual String toString () const
 Returns a string representation of this thread.
virtual void exit ()
 This method is called by the system to give a Thread a chance to clean up before it actually exits.

Static Public Member Functions

static ThreadcurrentThread ()
 Returns a reference to the currently executing thread object.
static void yield ()
 Causes the currently executing thread object to temporarily pause and allow other threads to execute.
static void sleep (jlong millis)
 Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

Static Public Attributes

static const jint TMIN_PRIORITY
 The minimum priority that a thread can have.
static const jint TMAX_PRIORITY
 The default priority that is assigned to a thread.
static const jint TNORM_PRIORITY
 The maximum priority that a thread can have.

Constructor & Destructor Documentation

java::lang::Thread::Thread ( const Ref< Runnable > &  target = 0  ) 

Allocates a new Thread object.

This constructor has the same effect as Thread(null, null, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.

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

<blockquote>

     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++; 
             }
         }
     }
 
</blockquote>

Allocates a new Thread object. This constructor has the same effect as Thread(null, target, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.

Parameters:
target the object whose run method is called.

java::lang::Thread::Thread ( const Thread src  ) 


Member Function Documentation

static Thread& java::lang::Thread::currentThread (  )  [static]

Returns a reference to the currently executing thread object.

Returns:
the currently executing thread.

static void java::lang::Thread::yield (  )  [static]

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

static void java::lang::Thread::sleep ( jlong  millis  )  [static]

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.
Exceptions:
InterruptedException if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

Thread& java::lang::Thread::operator= ( const Thread  ) 

virtual void java::lang::Thread::start ( const Ref< Runnable > &  target  )  [virtual]

Causes this thread to begin execution on the assigned target.

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

Parameters:
target the object whose run method is called.
Exceptions:
IllegalThreadStateException if the thread was already started.
See also:
java.lang.Thread::run()

java.lang.Thread::stop()

virtual void java::lang::Thread::start (  )  [virtual]

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

Exceptions:
IllegalThreadStateException if the thread was already started.
See also:
java.lang.Thread::run()

java.lang.Thread::stop()

virtual void java::lang::Thread::run (  )  [virtual]

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.

Implements java::lang::Runnable.

jboolean java::lang::Thread::isAlive (  )  const

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.

void java::lang::Thread::setPriority ( jint  newPriority  ) 

Changes the priority of this thread.

First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.

Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group.

Parameters:
newPriority priority to set this thread to
Exceptions:
IllegalArgumentException If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.

jint java::lang::Thread::getPriority (  )  const

Returns this thread's priority.

Returns:
this thread's name.
See also:
setPriority

java.lang.Thread::setPriority(int)

void java::lang::Thread::join ( jlong  millis = 0  ) 

Waits at most millis milliseconds for this thread to die.

A timeout of 0 means to wait forever.

Parameters:
millis the time to wait in milliseconds.
Exceptions:
InterruptedException if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

virtual String java::lang::Thread::toString (  )  const [virtual]

Returns a string representation of this thread.

Returns:
a string representation of this thread.

Reimplemented from java::lang::Object.

virtual void java::lang::Thread::exit (  )  [virtual]

This method is called by the system to give a Thread a chance to clean up before it actually exits.


Member Data Documentation

const jint java::lang::Thread::TMIN_PRIORITY [static]

The minimum priority that a thread can have.

const jint java::lang::Thread::TMAX_PRIORITY [static]

The default priority that is assigned to a thread.

const jint java::lang::Thread::TNORM_PRIORITY [static]

The maximum priority that a thread can have.


The documentation for this class was generated from the following file:
Generated on Fri May 16 11:56:42 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3