java.lang
Class Thread

java.lang.Object
  extended byjava.lang.Thread
All Implemented Interfaces:
Runnable

public class Thread
extends java.lang.Object
implements Runnable

Author:
josson smith 2006-5-5

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 default priority that is assigned to a thread.
 
Constructor Summary
Thread()
           
Thread(Runnable target)
          Allocates a new Thread object.
Thread(Runnable target, java.lang.String name)
          Allocates a new Thread object.
Thread(java.lang.String name)
          Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target)
          Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target, java.lang.String name)
          Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
Thread(ThreadGroup group, Runnable target, java.lang.String name, long stackSize)
          Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, and has the specified stack size.
Thread(ThreadGroup group, java.lang.String name)
          Allocates a new Thread object.
 
Method Summary
static Thread currentThread()
          Returns a reference to the currently executing thread object.
 java.lang.String getName()
          Returns this thread's name.
 int getPriority()
          Returns this thread's priority.
 ThreadGroup getThreadGroup()
          Returns the thread group to which this thread belongs.
 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 setName(java.lang.String name)
          Changes the name of this thread to be equal to the argument name.
 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.
 java.lang.String toString()
          Returns a string representation of this thread, including the thread's name, priority, and thread group.
 
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.

See Also:
Constant Field Values

NORM_PRIORITY

public static final int NORM_PRIORITY
The default priority that is assigned to a thread.

See Also:
Constant Field Values

MAX_PRIORITY

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

See Also:
Constant Field Values
Constructor Detail

Thread

public Thread()

Thread

public Thread(Runnable target)
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.
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)

Thread

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

Parameters:
group - the thread group.
target - the object whose run method is called.
Throws:
SecurityException - if the current thread cannot create a thread in the specified thread group.
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)

Thread

public Thread(java.lang.String name)
Allocates a new Thread object. This constructor has the same effect as Thread(null, null, name).

Parameters:
name - the name of the new thread.
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)

Thread

public Thread(ThreadGroup group,
              java.lang.String name)
Allocates a new Thread object. This constructor has the same effect as Thread(group, null, name)

Parameters:
group - the thread group.
name - the name of the new thread.
Throws:
SecurityException - if the current thread cannot create a thread in the specified thread group.
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)

Thread

public Thread(Runnable target,
              java.lang.String name)
Allocates a new Thread object. This constructor has the same effect as Thread(null, target, name).

Parameters:
target - the object whose run method is called.
name - the name of the new thread.
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)

Thread

public Thread(ThreadGroup group,
              Runnable target,
              java.lang.String name)
Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.

If group is null and there is a security manager, the group is determined by the security manager's getThreadGroup method. If group is null and there is not a security manager, or the security manager's getThreadGroup method returns null, the group is set to be the same ThreadGroup as the thread that is creating the new thread.

If there is a security manager, its checkAccess method is called with the ThreadGroup as its argument. This may result in a SecurityException.

If the target argument is not null, the run method of the target is called when this thread is started. If the target argument is null, this thread's run method is called when this thread is started.

The priority of the newly created thread is set equal to the priority of the thread creating it, that is, the currently running thread. The method setPriority may be used to change the priority to a new value.

The newly created thread is initially marked as being a daemon thread if and only if the thread creating it is currently marked as a daemon thread. The method setDaemon may be used to change whether or not a thread is a daemon.

Parameters:
group - the thread group.
target - the object whose run method is called.
name - the name of the new thread.
Throws:
SecurityException - if the current thread cannot create a thread in the specified thread group.
See Also:
Runnable.run(), run(), java.lang.Thread#setDaemon(boolean), setPriority(int), java.lang.ThreadGroup#checkAccess(), SecurityManager.checkAccess(java.lang.Thread)

Thread

public Thread(ThreadGroup group,
              Runnable target,
              java.lang.String name,
              long stackSize)
Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, and has the specified stack size.

This constructor is identical to Thread(ThreadGroup,Runnable,String) with the exception of the fact that it allows the thread stack size to be specified. The stack size is the approximate number of bytes of address space that the virtual machine is to allocate for this thread's stack. The effect of the stackSize parameter, if any, is highly platform dependent.

On some platforms, specifying a higher value for the stackSize parameter may allow a thread to achieve greater recursion depth before throwing a StackOverflowError. Similarly, specifying a lower value may allow a greater number of threads to exist concurrently without throwing an an OutOfMemoryError (or other internal error). The details of the relationship between the value of the stackSize parameter and the maximum recursion depth and concurrency level are platform-dependent. On some platforms, the value of the stackSize parameter may have no effect whatsoever.

The virtual machine is free to treat the stackSize parameter as a suggestion. If the specified value is unreasonably low for the platform, the virtual machine may instead use some platform-specific minimum value; if the specified value is unreasonably high, the virtual machine may instead use some platform-specific maximum. Likewise, the virtual machine is free to round the specified value up or down as it sees fit (or to ignore it completely).

Specifying a value of zero for the stackSize parameter will cause this constructor to behave exactly like the Thread(ThreadGroup, Runnable, String) constructor.

Due to the platform-dependent nature of the behavior of this constructor, extreme care should be exercised in its use. The thread stack size necessary to perform a given computation will likely vary from one JRE implementation to another. In light of this variation, careful tuning of the stack size parameter may be required, and the tuning may need to be repeated for each JRE implementation on which an application is to run.

Implementation note: Java platform implementers are encouraged to document their implementation's behavior with respect to the stackSize parameter.

Parameters:
group - the thread group.
target - the object whose run method is called.
name - the name of the new thread.
stackSize - the desired stack size for the new thread, or zero to indicate that this parameter is to be ignored.
Throws:
SecurityException - if the current thread cannot create a thread in the specified thread group.
Method Detail

currentThread

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

Returns:
the currently executing thread.

sleep

public static void sleep(long millis)
                  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:
millis - 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()

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(), java.lang.Thread#stop()

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.

Specified by:
run in interface Runnable
See Also:
start(), java.lang.Thread#stop(), Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String), Runnable.run()

setPriority

public final void setPriority(int 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
Throws:
IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
SecurityException - if the current thread cannot modify this thread.
See Also:
getPriority(), java.lang.Thread#checkAccess(), getPriority(), getThreadGroup(), MAX_PRIORITY, MIN_PRIORITY, ThreadGroup.getMaxPriority()

getPriority

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

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

setName

public final void setName(java.lang.String name)
Changes the name of this thread to be equal to the argument name.

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

Parameters:
name - the new name for this thread.
Throws:
SecurityException - if the current thread cannot modify this thread.
See Also:
getName(), java.lang.Thread#checkAccess(), getName()

getName

public final java.lang.String getName()
Returns this thread's name.

Returns:
this thread's name.
See Also:
setName(java.lang.String), setName(java.lang.String)

getThreadGroup

public final ThreadGroup getThreadGroup()
Returns the thread group to which this thread belongs. This method returns null if this thread has died (been stopped).

Returns:
this thread's thread group.

toString

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

Returns:
a string representation of this thread.