public class Thread extends Object implements Runnable
Object
,
ThreadGroup
Modifier and Type | Class and Description |
---|---|
static class |
Thread.State
The possible Thread states.
|
static interface |
Thread.UncaughtExceptionHandler
A handler which is invoked when an uncaught exception occurs in a Thread.
|
Modifier and Type | Field and Description |
---|---|
static int |
MAX_PRIORITY
The maximum priority value for a Thread.
|
static int |
MIN_PRIORITY
The minimum priority value for a Thread.
|
static int |
NORM_PRIORITY
The default priority value for a Thread.
|
Constructor and Description |
---|
Thread()
Constructs a new Thread with no runnable object and a newly generated name.
|
Thread(Runnable runnable)
Constructs a new Thread with a runnable object and a newly generated name.
|
Thread(Runnable runnable,
String threadName)
Constructs a new Thread with a runnable object and name provided.
|
Thread(String threadName)
Constructs a new Thread with no runnable object and the name provided.
|
Thread(ThreadGroup group,
Runnable runnable)
Constructs a new Thread with a runnable object and a newly generated name.
|
Thread(ThreadGroup group,
Runnable runnable,
String threadName)
Constructs a new Thread with a runnable object, the given name and
belonging to the ThreadGroup passed as parameter.
|
Thread(ThreadGroup group,
Runnable runnable,
String threadName,
long stack)
Constructs a new Thread with a runnable object, the given name and
belonging to the ThreadGroup passed as parameter.
|
Thread(ThreadGroup group,
String threadName)
Constructs a new Thread with no runnable object, the given name and
belonging to the ThreadGroup passed as parameter.
|
Modifier and Type | Method and Description |
---|---|
static int |
activeCount()
Returns how many threads are active in the
ThreadGroup
which the current thread belongs to. |
void |
checkAccess()
This method is used for operations that require approval from
a SecurityManager.
|
protected Object |
clone()
Answers a new instance of the same class as the receiver,
whose slots have been filled in with the values in the
slots of the receiver.
|
int |
countStackFrames()
Deprecated.
The semantics of this method are poorly defined and it uses the deprecated suspend() method.
|
static Thread |
currentThread()
Answers the instance of Thread that corresponds to the running Thread
which calls this method.
|
void |
destroy()
Deprecated.
May cause deadlocks.
|
static void |
dumpStack()
Prints a text representation of the stack for this Thread.
|
static int |
enumerate(Thread[] threads)
Copies an array with all Threads which are in the same ThreadGroup as
the receiver - and subgroups - into the array
threads
passed as parameter. |
static Map<Thread,StackTraceElement[]> |
getAllStackTraces()
Returns a Map containing Thread keys, and values which are arrays of StackTraceElement.
|
ClassLoader |
getContextClassLoader()
Returns the context ClassLoader for the receiver.
|
static Thread.UncaughtExceptionHandler |
getDefaultUncaughtExceptionHandler()
Return the default UncaughtExceptionHandler used for new Threads.
|
long |
getId()
Return a unique id for this Thread.
|
String |
getName()
Answers the name of the receiver.
|
int |
getPriority()
Answers the priority of the receiver.
|
StackTraceElement[] |
getStackTrace()
Returns an array of StackTraceElement, where each element of the array represents a frame
on the Java stack.
|
Thread.State |
getState()
Returns the current Thread state.
|
ThreadGroup |
getThreadGroup()
Answers the ThreadGroup to which the receiver belongs
|
Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
Return the UncaughtExceptionHandler for this Thread.
|
static boolean |
holdsLock(Object object)
Returns whether the current thread has a monitor lock on the specified object.
|
void |
interrupt()
Posts an interrupt request to the receiver
|
static boolean |
interrupted()
Answers a
boolean indicating whether the current Thread
(currentThread() ) has a pending interrupt request
(true ) or not (false ). |
boolean |
isAlive()
Answers
true if the receiver has
already been started and still runs code (hasn't died yet). |
boolean |
isDaemon()
Answers a
boolean indicating whether the receiver
is a daemon Thread (true ) or not (false )
A daemon Thread only runs as long as there are non-daemon Threads
running. |
boolean |
isInterrupted()
Answers a
boolean indicating whether the receiver
has a pending interrupt request (true ) or not (false ) |
void |
join()
Blocks the current Thread (
Thread.currentThread() ) until the
receiver finishes its execution and dies. |
void |
join(long timeoutInMilliseconds)
Blocks the current Thread (
Thread.currentThread() ) until the
receiver finishes its execution and dies or the specified timeout expires, whatever
happens first. |
void |
join(long timeoutInMilliseconds,
int nanos)
Blocks the current Thread (
Thread.currentThread() ) until the
receiver finishes its execution and dies or the specified timeout expires, whatever
happens first. |
void |
resume()
Deprecated.
Used with deprecated method Thread.suspend().
|
void |
run()
Calls the
run() method of the Runnable object the receiver holds. |
void |
setContextClassLoader(ClassLoader cl)
Set the context ClassLoader for the receiver.
|
void |
setDaemon(boolean isDaemon)
Set if the receiver is a daemon Thread or not.
|
static void |
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Set the UncaughtExceptionHandler used for new Threads.
|
void |
setName(String threadName)
Sets the name of the receiver.
|
void |
setPriority(int requestedPriority)
Sets the priority of the receiver.
|
void |
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Set the UncaughtExceptionHandler for this Thread.
|
static void |
sleep(long time)
Causes the thread which sent this message to sleep an interval
of time (given in milliseconds).
|
static void |
sleep(long time,
int nanos)
Causes the thread which sent this message to sleep an interval
of time (given in milliseconds).
|
void |
start()
Starts the new Thread of execution.
|
void |
stop()
Deprecated.
|
void |
stop(Throwable throwable)
Deprecated.
|
void |
suspend()
Deprecated.
May cause deadlocks.
|
String |
toString()
Answers a string containing a concise, human-readable
description of the receiver.
|
static void |
yield()
Causes the thread which sent this message to yield execution to another Thread
that is ready to run.
|
public static final int MAX_PRIORITY
public static final int MIN_PRIORITY
public static final int NORM_PRIORITY
public Thread()
ThreadGroup
public Thread(Runnable runnable)
runnable
- a java.lang.Runnable whose method run
will be executed by the new ThreadThreadGroup
,
Runnable
public Thread(Runnable runnable, String threadName)
runnable
- a java.lang.Runnable whose method run
will be executed by the new ThreadthreadName
- Name for the Thread being createdThreadGroup
,
Runnable
public Thread(String threadName)
threadName
- Name for the Thread being createdThreadGroup
,
Runnable
public Thread(ThreadGroup group, Runnable runnable)
group
- ThreadGroup to which the new Thread will belongrunnable
- a java.lang.Runnable whose method run
will be executed by the new ThreadSecurityException
- if group.checkAccess()
fails with a SecurityExceptionIllegalThreadStateException
- if group.destroy()
has already been doneThreadGroup
,
Runnable
,
SecurityException
,
SecurityManager
public Thread(ThreadGroup group, Runnable runnable, String threadName, long stack)
group
- ThreadGroup to which the new Thread will belongrunnable
- a java.lang.Runnable whose method run
will be executed by the new ThreadthreadName
- Name for the Thread being createdstack
- Platform dependent stack sizeSecurityException
- if group.checkAccess()
fails with a SecurityExceptionIllegalThreadStateException
- if group.destroy()
has already been doneThreadGroup
,
Runnable
,
SecurityException
,
SecurityManager
public Thread(ThreadGroup group, Runnable runnable, String threadName)
group
- ThreadGroup to which the new Thread will belongrunnable
- a java.lang.Runnable whose method run
will be executed by the new ThreadthreadName
- Name for the Thread being createdSecurityException
- if group.checkAccess()
fails with a SecurityExceptionIllegalThreadStateException
- if group.destroy()
has already been doneThreadGroup
,
Runnable
,
SecurityException
,
SecurityManager
public Thread(ThreadGroup group, String threadName)
group
- ThreadGroup to which the new Thread will belongthreadName
- Name for the Thread being createdSecurityException
- if group.checkAccess()
fails with a SecurityExceptionIllegalThreadStateException
- if group.destroy()
has already been doneThreadGroup
,
SecurityException
,
SecurityManager
public static int activeCount()
ThreadGroup
which the current thread belongs to.public final void checkAccess()
checkAccess(Ljava.lang.Thread;)
is called for that SecurityManager.SecurityException
,
SecurityManager
@Deprecated public int countStackFrames()
public static Thread currentThread()
currentThread()
@Deprecated public void destroy()
public static void dumpStack()
public static int enumerate(Thread[] threads)
threads
passed as parameter. If the array passed as parameter is too small no
exception is thrown - the extra elements are simply not copied.threads
- array into which the Threads will be copiedSecurityException
- if the installed SecurityManager fails checkAccess(Ljava.lang.Thread;)
SecurityException
,
SecurityManager
public ClassLoader getContextClassLoader()
ClassLoader
,
getContextClassLoader()
public final String getName()
public final int getPriority()
int
)setPriority(int)
public final ThreadGroup getThreadGroup()
public void interrupt()
SecurityException
- if group.checkAccess()
fails with a SecurityExceptionSecurityException
,
SecurityManager
,
interrupted()
,
isInterrupted()
public static boolean interrupted()
boolean
indicating whether the current Thread
(currentThread()
) has a pending interrupt request
(true
) or not (false
). It also has the
side-effect of clearing the flag.boolean
currentThread()
,
interrupt()
,
isInterrupted()
public final boolean isAlive()
true
if the receiver has
already been started and still runs code (hasn't died yet).
Answers false
either if the receiver hasn't been
started yet or if it has already started and run to completion and died.boolean
start()
public final boolean isDaemon()
boolean
indicating whether the receiver
is a daemon Thread (true
) or not (false
)
A daemon Thread only runs as long as there are non-daemon Threads
running. When the last non-daemon Thread ends, the whole program ends
no matter if it had daemon Threads still running or not.boolean
setDaemon(boolean)
public boolean isInterrupted()
boolean
indicating whether the receiver
has a pending interrupt request (true
) or not (false
)boolean
interrupt()
,
interrupted()
public final void join() throws InterruptedException
Thread.currentThread()
) until the
receiver finishes its execution and dies.InterruptedException
- if interrupt()
was called for the receiver while
it was in the join()
callObject.notifyAll()
,
ThreadDeath
public final void join(long timeoutInMilliseconds) throws InterruptedException
Thread.currentThread()
) until the
receiver finishes its execution and dies or the specified timeout expires, whatever
happens first.timeoutInMilliseconds
- The maximum time to wait (in milliseconds).InterruptedException
- if interrupt()
was called for the receiver while
it was in the join()
callObject.notifyAll()
,
ThreadDeath
public final void join(long timeoutInMilliseconds, int nanos) throws InterruptedException
Thread.currentThread()
) until the
receiver finishes its execution and dies or the specified timeout expires, whatever
happens first.timeoutInMilliseconds
- The maximum time to wait (in milliseconds).nanos
- Extra nanosecond precisionInterruptedException
- if interrupt()
was called for the receiver while
it was in the join()
callObject.notifyAll()
,
ThreadDeath
@Deprecated public final void resume()
SecurityException
- if checkAccess()
fails with a SecurityExceptionsuspend()
public void run()
run()
method of the Runnable object the receiver holds.
If no Runnable is set, does nothing.public void setContextClassLoader(ClassLoader cl)
cl
- The context ClassLoaderClassLoader
,
getContextClassLoader()
public final void setDaemon(boolean isDaemon)
isDaemon
- A boolean indicating if the Thread should be daemon or notSecurityException
- if checkAccess()
fails with a SecurityExceptionisDaemon
public final void setName(String threadName)
threadName
- new name for the ThreadSecurityException
- if checkAccess()
fails with a SecurityExceptiongetName()
public final void setPriority(int requestedPriority)
requestedPriority
- new priority for the ThreadSecurityException
- if checkAccess()
fails with a SecurityExceptionIllegalArgumentException
- if the new priority is greater than Thread.MAX_PRIORITY or less than
Thread.MIN_PRIORITYgetPriority()
public static void sleep(long time) throws InterruptedException
time
- The time to sleep in milliseconds.InterruptedException
- if interrupt()
was called for this Thread while it was sleepinginterrupt()
public static void sleep(long time, int nanos) throws InterruptedException
time
- The time to sleep in milliseconds.nanos
- Extra nanosecond precisionInterruptedException
- if interrupt()
was called for this Thread while it was sleepinginterrupt()
public void start()
run()
method of the receiver
will be called by the receiver Thread itself (and not the Thread calling start()
).IllegalThreadStateException
- Unspecified in the Java language specificationrun()
@Deprecated public final void stop()
SecurityException
- if checkAccess()
fails with a SecurityException@Deprecated public final void stop(Throwable throwable)
throwable
- Throwable object to be thrown by the Thread@Deprecated public final void suspend()
isAlive()
however, suspended it until resume()
is sent to it. Suspend requests
are not queued, which means that N requests are equivalent to just one - only one
resume request is needed in this case.SecurityException
- if checkAccess()
fails with a SecurityExceptionresume()
public String toString()
public static void yield()
public static boolean holdsLock(Object object)
object
- the object to test for the monitor lockpublic StackTraceElement[] getStackTrace()
SecurityException
- if the RuntimePermission "getStackTrace" is not allowedStackTraceElement
public static Map<Thread,StackTraceElement[]> getAllStackTraces()
SecurityException
- if the RuntimePermission "getStackTrace" is not allowed, or the
RuntimePermission "modifyThreadGroup" is not allowedgetStackTrace()
public long getId()
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Thread.UncaughtExceptionHandler
public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
handler
- the UncaughtExceptionHandler to setThread.UncaughtExceptionHandler
public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
Thread.UncaughtExceptionHandler
public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
handler
- the UncaughtExceptionHandler to setThread.UncaughtExceptionHandler
public Thread.State getState()
Thread.State
protected Object clone() throws CloneNotSupportedException
Object
Classes which wish to support cloning must specify that they implement the Cloneable interface, since the native implementation checks for this.
clone
in class Object
CloneNotSupportedException
- if the receiver's class does not implement
the interface Cloneable.Eclipse OpenJ9 website.
To raise a bug report or suggest an improvement create an Eclipse OpenJ9 issue.
Copyright © 1993, 2023 IBM Corp. and others.