GeezerVison is showing docs.oracle.com — Thread (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.lang
Class Thread
-
-
Nested Class Summary
Nested Classes
| Modifier and Type |
Class |
Description |
static class |
Thread.State |
A thread state.
|
static interface |
Thread.UncaughtExceptionHandler |
Interface for handlers invoked when a Thread abruptly
terminates due to an uncaught exception.
|
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
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
Constructors
| Constructor |
Description |
Thread() |
Allocates a new Thread object.
|
Thread(Runnable target) |
Allocates a new Thread object.
|
Thread(Runnable target,
String name) |
Allocates a new Thread object.
|
Thread(String name) |
Allocates a new Thread object.
|
Thread(ThreadGroup group,
Runnable target) |
Allocates a new Thread object.
|
Thread(ThreadGroup group,
Runnable target,
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,
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,
and belongs to the thread group referred to by group, and has
the specified stack size.
|
Thread(ThreadGroup group,
String name) |
Allocates a new Thread object.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
| Modifier and Type |
Method |
Description |
static int |
activeCount() |
Returns an estimate of the number of active threads in the current
thread's thread group and its
subgroups.
|
void |
checkAccess() |
Determines if the currently running thread has permission to
modify this thread.
|
protected Object |
clone() |
Throws CloneNotSupportedException as a Thread can not be meaningfully
cloned.
|
int |
countStackFrames() |
Deprecated.
The definition of this call depends on suspend(),
which is deprecated. Further, the results of this call
were never well-defined.
|
static Thread |
currentThread() |
Returns a reference to the currently executing thread object.
|
void |
destroy() |
Deprecated.
This method was originally designed to destroy this
thread without any cleanup. Any monitors it held would have
remained locked. However, the method was never implemented.
If if were to be implemented, it would be deadlock-prone in
much the manner of suspend(). If the target thread held
a lock protecting a critical system resource when it was
destroyed, no thread could ever access this resource again.
If another thread ever attempted to lock this resource, deadlock
would result. Such deadlocks typically manifest themselves as
"frozen" processes. For more information, see
Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
|
static void |
dumpStack() |
Prints a stack trace of the current thread to the standard error stream.
|
static int |
enumerate(Thread[] tarray) |
Copies into the specified array every active thread in the current
thread's thread group and its subgroups.
|
static Map<Thread,StackTraceElement[]> |
getAllStackTraces() |
Returns a map of stack traces for all live threads.
|
ClassLoader |
getContextClassLoader() |
Returns the context ClassLoader for this Thread.
|
static Thread.UncaughtExceptionHandler |
getDefaultUncaughtExceptionHandler() |
Returns the default handler invoked when a thread abruptly terminates
due to an uncaught exception.
|
long |
getId() |
Returns the identifier of this Thread.
|
String |
getName() |
Returns this thread's name.
|
int |
getPriority() |
Returns this thread's priority.
|
StackTraceElement[] |
getStackTrace() |
Returns an array of stack trace elements representing the stack dump
of this thread.
|
Thread.State |
getState() |
Returns the state of this thread.
|
ThreadGroup |
getThreadGroup() |
Returns the thread group to which this thread belongs.
|
Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler() |
Returns the handler invoked when this thread abruptly terminates
due to an uncaught exception.
|
static boolean |
holdsLock(Object obj) |
Returns true if and only if the current thread holds the
monitor lock on the specified object.
|
void |
interrupt() |
Interrupts this thread.
|
static boolean |
interrupted() |
Tests whether the current thread has been interrupted.
|
boolean |
isAlive() |
Tests if this thread is alive.
|
boolean |
isDaemon() |
Tests if this thread is a daemon thread.
|
boolean |
isInterrupted() |
Tests whether this thread has been interrupted.
|
void |
join() |
Waits for this thread to die.
|
void |
join(long millis) |
Waits at most millis milliseconds for this thread to
die.
|
void |
join(long millis,
int nanos) |
Waits at most millis milliseconds plus
nanos nanoseconds for this thread to die.
|
void |
resume() |
|
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 |
setContextClassLoader(ClassLoader cl) |
Sets the context ClassLoader for this Thread.
|
void |
setDaemon(boolean on) |
Marks this thread as either a daemon thread
or a user thread.
|
static void |
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) |
Set the default handler invoked when a thread abruptly terminates
due to an uncaught exception, and no other handler has been defined
for that thread.
|
void |
setName(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.
|
void |
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) |
Set the handler invoked when this thread abruptly terminates
due to an uncaught exception.
|
static void |
sleep(long millis) |
Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds, subject to
the precision and accuracy of system timers and schedulers.
|
static void |
sleep(long millis,
int nanos) |
Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds plus the specified
number of nanoseconds, subject to the precision and accuracy of system
timers and schedulers.
|
void |
start() |
Causes this thread to begin execution; the Java Virtual Machine
calls the run method of this thread.
|
void |
stop() |
Deprecated.
This method is inherently unsafe. Stopping a thread with
Thread.stop causes it to unlock all of the monitors that it
has locked (as a natural consequence of the unchecked
ThreadDeath exception propagating up the stack). If
any of the objects previously protected by these monitors were in
an inconsistent state, the damaged objects become visible to
other threads, potentially resulting in arbitrary behavior. Many
uses of stop should be replaced by code that simply
modifies some variable to indicate that the target thread should
stop running. The target thread should check this variable
regularly, and return from its run method in an orderly fashion
if the variable indicates that it is to stop running. If the
target thread waits for long periods (on a condition variable,
for example), the interrupt method should be used to
interrupt the wait.
For more information, see
Why
are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
|
void |
stop(Throwable obj) |
|
void |
suspend() |
Deprecated.
This method has been deprecated, as it is
inherently deadlock-prone. If the target thread holds a lock on the
monitor protecting a critical system resource when it is suspended, no
thread can access this resource until the target thread is resumed. If
the thread that would resume the target thread attempts to lock this
monitor prior to calling resume, deadlock results. Such
deadlocks typically manifest themselves as "frozen" processes.
For more information, see
Why
are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
|
String |
toString() |
Returns a string representation of this thread, including the
thread's name, priority, and thread group.
|
static void |
yield() |
A hint to the scheduler that the current thread is willing to yield
its current use of a processor.
|
-
-
Constructor Detail
-
Thread
public Thread()
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.
-
Method Detail
-
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, subject to
the precision and accuracy of system timers and schedulers. The thread
does not lose ownership of any monitors.
- Parameters:
millis - the length of time to sleep in milliseconds
- Throws:
IllegalArgumentException - if the value of millis is negative
InterruptedException - if any thread has interrupted the current thread. The
interrupted status of the current thread is
cleared when this exception is thrown.
-
sleep
public static void sleep(long millis,
int nanos)
throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds plus the specified
number of nanoseconds, subject to the precision and accuracy of system
timers and schedulers. The thread does not lose ownership of any
monitors.
- Parameters:
millis - the length of time to sleep in milliseconds
nanos - 0-999999 additional nanoseconds to sleep
- Throws:
IllegalArgumentException - if the value of millis is negative, or the value of
nanos is not in the range 0-999999
InterruptedException - if any thread has interrupted the current thread. The
interrupted status of the current thread is
cleared when this exception is thrown.
-
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).
It is never legal to start a thread more than once.
In particular, a thread may not be restarted once it has completed
execution.
- Throws:
IllegalThreadStateException - if the thread was already
started.
- See Also:
run(),
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(),
stop(),
Thread(ThreadGroup, Runnable, String)
-
interrupt
public void interrupt()
Interrupts this thread.
Unless the current thread is interrupting itself, which is
always permitted, the checkAccess method
of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object
class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int),
methods of this class, then its interrupt status will be cleared and it
will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an InterruptibleChannel
then the channel will be closed, the thread's interrupt
status will be set, and the thread will receive a ClosedByInterruptException.
If this thread is blocked in a Selector
then the thread's interrupt status will be set and it will return
immediately from the selection operation, possibly with a non-zero
value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt
status will be set.
Interrupting a thread that is not alive need not have any effect.
- Throws:
SecurityException - if the current thread cannot modify this thread
-
toString
public String toString()
Returns a string representation of this thread, including the
thread's name, priority, and thread group.
- Overrides:
toString in class Object
- Returns:
- a string representation of this thread.
-
getState
public Thread.State getState()
Returns the state of this thread.
This method is designed for use in monitoring of the system state,
not for synchronization control.
- Returns:
- this thread's state.
- Since:
- 1.5
-
getUncaughtExceptionHandler
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Returns the handler invoked when this thread abruptly terminates
due to an uncaught exception. If this thread has not had an
uncaught exception handler explicitly set then this thread's
ThreadGroup object is returned, unless this thread
has terminated, in which case null is returned.
- Returns:
- the uncaught exception handler for this thread
- Since:
- 1.5
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|