public class Thread
- Object
- Thread
ImplementsRunnable
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.
Fields
public static final int MAX_PRIORITY = 10 | The maximum priority that a thread can have. |
public static final int MIN_PRIORITY = 1 | The minimum priority that a thread can have. |
public static final int NORM_PRIORITY = 5 | The default priority that is assigned to a thread. |
Constructors
public Thread() | Allocates a new Thread object. |
public Thread(Runnable target) | Allocates a new Thread object with a specific target object whose run method is called. |
public Thread(Runnable target, String name) | Allocates a new Thread object with the given target and name. |
public Thread(String name) | Allocates a new Thread object with the given name. |
Methods
public static int activeCount() | Returns the current number of active threads in the virtual machine. |
public static Thread currentThread() | Returns a reference to the currently executing Thread object. |
public final String getName() | Returns this thread’s name. |
public final int getPriority() | Returns this thread’s priority. |
public void interrupt() | Interrupts this thread. |
public final boolean isAlive() | Tests if this thread is alive. |
public final void join()
throws InterruptedException | Waits for this thread to die. |
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. |
public final void setPriority(int newPriority) | Changes the priority of this thread. |
public static void sleep(long millis)
throws InterruptedException | Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. |
public void start() | Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. |
public String toString() | Returns a string representation of this thread, including the thread’s name and priority. |
public static void yield() | Causes the currently executing thread object to temporarily pause and allow other threads to execute. |
public StackTraceElement[] getStackTrace() |
Inherited methods
Field details
MAX_PRIORITY
public static final int MAX_PRIORITY = 10The maximum priority that a thread can have.
See Also:Constant Field Values
MIN_PRIORITY
public static final int MIN_PRIORITY = 1The minimum priority that a thread can have.
See Also:Constant Field Values
NORM_PRIORITY
public static final int NORM_PRIORITY = 5The default priority that is assigned to a thread.
See Also:Constant Field Values
Constructor details
Thread
public Thread()Allocates a new Thread object.
Threads created this way must have overridden their run() method to actually do anything.
Thread
public Thread(Runnable target)Allocates a new Thread object with a specific target object whose run method is called.
target - the object whose run method is called.
Thread
public Thread(Runnable target, String name)Allocates a new Thread object with the given target and name.
target - the object whose run method is called.name - the name of the new thread.
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.
name - the name of the new thread.
Method details
activeCount
public static int activeCount()Returns the current number of active threads in the virtual machine.
currentThread
public static Thread currentThread()Returns a reference to the currently executing Thread object.
getName
public final String getName()Returns this thread’s name. Note that in CLDC the name of the thread can only be set when creating the thread.
getPriority
public final int getPriority()Returns this thread’s priority.
interrupt
public void interrupt()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.
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.
join
public final void join()
throws InterruptedExceptionWaits for this thread to die.
Throws
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.
setPriority
public final void setPriority(int newPriority)Changes the priority of this thread.
sleep
public static void sleep(long millis)
throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.
Throws
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).
toString
public String toString()Returns a string representation of this thread, including the thread’s name and priority.
yield
public static void yield()Causes the currently executing thread object to temporarily pause and allow other threads to execute.
getStackTrace
public StackTraceElement[] getStackTrace()