Class java.lang.Thread
All Packages    This Package    Previous    Next

Class java.lang.Thread

java.lang.Object
   |
   +----java.lang.Thread

public class Thread
extends Object
implements Runnable
Thread objects are the basis for multi-threaded programming. To create a new thread of execution, declare a new class which is a subclass of Thread. Override the run() method with a method whose body is the code you want to execute in the thread. Then create an instance of the class and call the start() method to create the thread and run the run method. For example:

	class PrimeThread extends Thread {
	    public void run() {
		// compute primes...
	    }
	}
To start this thread you need to do the following:
	PrimeThread p = new PrimeThread();
	p.start();
	...
Another way to create a thread is by using the Runnable interface. This way any object that implements the Runnable interface can be run in a thread. For example:
	class Primes Implements Runnable {
	    public void run() {
		// compute primes...
	    }
	}
To start this thread you need to do the following:
	Primes p = new Primes();
	new Thread(p).start();
	...
The interpreter runs until all threads that are not deamon threads have died. A thread dies when its run method returns, or when the stop method is called.

When a new thread is created, it inherits the priority and the daemon flag from its parent (ie: the thread that created it).

Version:
1.26, 20 Feb 1995

MAX_PRIORITY
The maximum priority that a Thread can have.
MIN_PRIORITY
The minimum priority that a Thread can have.
NORM_PRIORITY
The default priority that is assigned to a Thread.

Thread()
Constructs a new thread.
Thread(Runnable)
Constructs a new thread that applies the run method to the specified target.
Thread(String)
Constructs a new thread with the specified name.
Thread(Runnable, String)
Constructs a new thread with the specified name and applies the run() method on the specified target.

activeCount()
Returns the current number of active threads.
countStackFrames()
Returns the number of stack frames in this thread.
currentThread()
Returns a reference to the currently executing thread object.
dumpStack()
A debugging procedure to print a stack trace for the current thread.
enumerate(Thread[])
Copies references to every active thread into an array.
getName()
Returns the thread's name.
getPriority()
Returns the thread's priority.
isAlive()
Returns a boolean indicating if the thread is active.
isDaemon()
Returns the deamon flag of the thread.
join(int)
Waits for this thread to die.
join()
Waits forever for this thread to die.
postException(Object)
Post an object to another thread, to be thrown when it resumes.
resume()
Resumes the thread execution.
run()
The body of the thread.
setDaemon(boolean)
Marks this thread as a daemon thread or a user thread.
setName(String)
Sets the thread's name.
setPriority(int)
Sets the thread's priority.
setThreadPriority(int)
sleep(int)
Causes the currently executing thread to sleep for the specified number of milliseconds.
start()
Starts a thread.
stop()
Stops a thread by tossing an object.
stop(Object)
Stops a thread by tossing an object.
suspend()
Suspends the thread execution.
yield()
Causes the currently executing thread object to yield.

MIN_PRIORITY
  public final static int MIN_PRIORITY
The minimum priority that a Thread can have.
NORM_PRIORITY
  public final static int NORM_PRIORITY
The default priority that is assigned to a Thread.
MAX_PRIORITY
  public final static int MAX_PRIORITY
The maximum priority that a Thread can have.

Thread
  public Thread()
Constructs a new thread. Threads created this way must have overridden their run() method to actually do anything.

Thread

  public Thread(Runnable target)
Constructs a new thread that applies the run method to the specified target.
Parameters:
target - the object who's run method is called

Thread

  public Thread(String name)
Constructs a new thread with the specified name.
Parameters:
name - the name of the new thread

Thread

  public Thread(Runnable target,
                String name)
Constructs a new thread with the specified name and applies the run() method on the specified target.
Parameters:
target - the object whose run method is called
name - the name of the new thread

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

yield

  public static void yield()
Causes the currently executing thread object to yield. (i.e., if there are other runnable threads they will be scheduled next).

sleep

  public static void sleep(int millis)
Causes the currently executing thread to sleep for the specified number of milliseconds.
Parameters:
- millis the length of time to sleep in milliseconds

start

  public synchronized void start()
Starts a thread. This will cause the run() method to be called. This method will return immediately.
Throws: IllegalStateException
The thread was already started
See Also:
run, stop

postException

  public void postException(Object exception)
Post an object to another thread, to be thrown when it resumes. If the object being posted is an instance of class Exception the stack trace of the thread being posted to will be filled in the instance. This routine is used by Thread.stop() to asynchronously terminate threads.
Parameters:
instance - to post
Throws: IllegalStateException
If the target thread is not started

run

  public void run()
The body of the thread. This method is called after the thread is started. You must either override this method by subclassing class Thread, or you must create the thread with a target.
See Also:
start, stop

stop

  public synchronized void stop()
Stops a thread by tossing an object. By default this routine tosses a new instance of ThreadDeath to the target thread. ThreadDeath is not actually a subclass of Exception, but is a subclass of Object. Users should not normally try to catch ThreaDeath unless they must do some extraordinary cleanup operation. If ThreadDeath is caught it is important to rethrow the object so that the thread will actually die. The top-level error handler will not print out a message if ThreadDeath falls through. The essential difference between this routine and postException() is that if the target thread has not yet started to run it will be killed imediately without trying to post the object instance as an error.
Throws: IllegalStateException
If the thread is not started
See Also:
start , run

stop

  public synchronized void stop(Object o)
Stops a thread by tossing an object. Normally users should just call Thread.Stop() with no argument. In some exceptional circumstances Used by Thread.Stop() to kill anothe is tossed, "ThreadDeath", is not actually a subclass of Exception, but is a subclass of Object. The essential difference between this routine and postException() is that if the target thread has not yet started to run it will be killed imediately without trying to post the object instance as an error.
Throws: IllegalStateException
If the thread is not started
See Also:
start , run

isAlive

  public boolean isAlive()
Returns a boolean indicating if the thread is active.
Returns:
a boolean indicating whether the thread has been started

suspend

  public void suspend()
Suspends the thread execution.
Throws: IllegalStateException
The thread is not active.

resume

  public void resume()
Resumes the thread execution.
Throws: IllegalStateException
The thread is not active.

setPriority

  public void setPriority(int newPriority)
Sets the thread's priority.
Throws: IllegalArgumentException
The priority is not within the range MIN_PRIORITY, MAX_PRIORITY
See Also:
MIN_PRIORITY, MAX_PRIORITY, getPriority

setThreadPriority

  public void setThreadPriority(int newPriority)
getPriority
  public int getPriority()
Returns the thread's priority.
Returns:
the priority of the thread
See Also:
setPriority

setName

  public void setName(String name)
Sets the thread's name.
Parameters:
name - the new name of the thread
See Also:
getName

getName

  public String getName()
Returns the thread's name.
Returns:
the name of the thread
See Also:
setName

activeCount

  public static int activeCount()
Returns the current number of active threads.
Returns:
integer count of active threads

enumerate

  public static int enumerate(Thread tarray[])
Copies references to every active thread into an array.
Returns:
number of Threads put into the array

countStackFrames

  public int countStackFrames()
Returns the number of stack frames in this thread. The thread must be suspended when this method is called.
Throws: IllegalStateException
The thread is not suspended

join

  public synchronized void join(int millis)
Waits for this thread to die. A timeout in milliseconds can be specified. A timeout of 0 milliseconds means to wait forever.
Parameters:
millis - the time to wait in milliseconds

join

  public void join()
Waits forever for this thread to die.

dumpStack

  public static void dumpStack()
A debugging procedure to print a stack trace for the current thread.
See Also:
printStackTrace

setDaemon

  public void setDaemon(boolean on)
Marks this thread as a daemon thread or a user thread. When there are only daemon threads left running in the system, Java exits.
Parameters:
on - determines whether the thread will be a deamon thread
Throws: IllegalStateException
The thread is active
See Also:
isDaemon

isDaemon

  public boolean isDaemon()
Returns the deamon flag of the thread.
Returns:
a boolean indicating wheter the thread is a deamon thread
See Also:
setDaemon


All Packages    This Package    Previous    Next