Java.lang.Thread class in Java

Thread a line of execution within a program. Each program can have multiple associated threads. Each thread has a priority which is used by thread scheduler to determine which thread must run first. Java provides a thread class that has various method calls inorder to manage the behaviour of threads.Note: Every class that is used as thread must implement Runnable interface and over ride it’s run method.

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

activeCount(): java.lang.Thread.activeCount() Returns an estimate of the number of active threads in the current thread’s thread group and its subgroups

Syntax:

public static int activeCount()
Returns:
an estimate of the number of active threads in the current
thread's thread group and in any other thread group that has
the current thread's thread group as an ancestor

checkAccess(): java.lang.Thread.checkAccess() Determines if the currently running thread has permission to modify this thread

Syntax:

public final void checkAccess()
Throws:
SecurityException - if the current thread is not allowed to
access this thread.

clone(): java.lang.Thread.clone() Throws CloneNotSupportedException as a Thread can not be meaningfully cloned

currentThread(): java.lang.Thread.currentThread() Returns a reference to the currently executing thread object

Syntax:

public static Thread currentThread()
Returns:
the currently executing thread

dumpStack(): java.lang.Thread.dumpStack() Prints a stack trace of the current thread to the standard error stream

Syntax:

public static void dumpStack()
Description:
Prints a stack trace of the current thread to the standard
error stream. This method is used only for debugging

enumerate(Thread[] tarray): java.lang.Thread.enumerate(Thread[] tarray) Copies into the specified array every active thread in the current thread’s thread group and its subgroups

Syntax:

public static int enumerate(Thread[] tarray)
Parameters:
tarray - an array into which to put the list of threads
Returns:
the number of threads put into the array
Throws:
SecurityException - if ThreadGroup.checkAccess()
determines that the current thread cannot
access its thread group

getAllStackTraces(): java.lang.Thread.getAllStackTraces() Returns a map of stack traces for all live threads

Syntax:

public static Map getAllStackTraces()
Returns:
a Map from Thread to an array of StackTraceElement that represents
the stack trace of the corresponding thread
Throws:
SecurityException - if a security manager exists and its checkPermission method
doesn't allow getting the stack trace of thread

getContextClassLoader(): java.lang.Thread.getContextClassLoader() Returns the context ClassLoader for this Thread

Syntax:

public ClassLoader getContextClassLoader()
Returns:
the context ClassLoader for this Thread, or null indicating the system class loader
(or, failing that, the bootstrap class loader)
Throws:
SecurityException - if the current thread cannot get the context ClassLoader

getDefaultUncaughtExceptionHandler(): java.lang.Thread.getDefaultUncaughtExceptionHandler() Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception

Syntax:

public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
Returns:
the default uncaught exception handler for all threads

getId(): java.lang.Thread.getId() Returns the identifier of this Thread

getStackTrace(): java.lang.Thread.getStackTrace() Returns an array of stack trace elements representing the stack dump of this thread

Syntax:

public StackTraceElement[] getStackTrace()
Returns:
an array of StackTraceElement, each represents one stack frame
Throws:
SecurityException - if a security manager exists and its
checkPermission method doesn't
allow getting the stack trace of thread

getState(): java.lang.Thread.getState() Returns the state of this thread

Syntax:

public Thread.State getState()
Returns:
this thread's state

getThreadGroup(): java.lang.Thread.getThreadGroup() Returns the thread group to which this thread belongs

Syntax:

public final ThreadGroup getThreadGroup()
Returns:
this thread's thread group

getUncaughtExceptionHandler(): java.lang.Thread.getUncaughtExceptionHandler() Returns the handler invoked when this thread abruptly terminates due to an uncaught exception

Syntax:

public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Returns:
the uncaught exception handler for this thread

holdsLock(Object obj): java.lang.Thread.holdsLock(Object obj) Returns true if and only if the current thread holds the monitor lock on the specified object

Syntax:

public static boolean holdsLock(Object obj)
Parameters:
obj - the object on which to test lock ownership
Returns:
true if the current thread holds the monitor lock
on the specified object.
Throws:
NullPointerException - if obj is null

interrupt(): java.lang.Thread.interrupt() Interrupts this thread

Syntax:

public void interrupt()
Throws:
SecurityException - if the current thread cannot modify this thread

interrupted(): java.lang.Thread.interrupted() Tests whether the current thread has been interrupted

Syntax:

public static boolean interrupted()
Returns:
true if the current thread has been interrupted; false otherwise

isAlive(): java.lang.Thread.isAlive() Tests if this thread is alive

Syntax:

public final boolean isAlive()
Returns:
true if this thread is alive; false otherwise

isDaemon(): java.lang.Thread.isDaemon() Tests if this thread is a daemon thread

Syntax:

public final boolean isDaemon()
Returns:
true if this thread is a daemon thread; false otherwise

isInterrupted(): java.lang.Thread.isInterrupted() Tests whether this thread has been interrupted

Syntax:

public boolean isInterrupted()
Returns:
true if this thread has been interrupted; false otherwise

join(): java.lang.Thread.join() Waits for this thread to die

Syntax:

public final void join() throws InterruptedException
Throws:
InterruptedException - if any thread has interrupted the
current thread. The interrupted status of the current thread is
cleared when this exception is thrown

join(long millis): java.lang.Thread.join(long millis) Waits at most millis milliseconds for this thread to die

Syntax:

public final void join(long millis) throws InterruptedException
Parameters:
millis - the time to wait 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.

run(): java.lang.Thread.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

Syntax:

public void run()
Description:
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.

yield(): java.lang.Thread.yield() A hint to the scheduler that the current thread is willing to yield its current use of a processor

Syntax:

public static void yield()
Description:
A hint to the scheduler that the current thread is willing
to yield its current use of a
processor. The scheduler is free to ignore this hint

toString(): java.lang.Thread.toString() Returns a string representation of this thread, including the thread’s name, priority, and thread group

Syntax:

public String toString()
Returns:
a string representation of this thread

start(): java.lang.Thread.start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread

Syntax:

public void start()
Throws:
IllegalThreadStateException - if the thread was already started.

sleep(long millis): java.lang.Thread.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

Syntax:

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

setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh): java.lang.Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) Set the handler invoked when this thread abruptly terminates due to an uncaught exception

Syntax:

public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
Parameters:
eh - the object to use as this thread's uncaught exception handler.
If null then this thread has no explicit handler.
Throws:
SecurityException - if the current thread is not allowed to
modify this thread.

setPriority(int newPriority): java.lang.Thread.setPriority(int newPriority) Changes the priority of this thread

Syntax:

public final void setPriority(int newPriority)
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.

setName(String name): java.lang.Thread.setName(String name) Changes the name of this thread to be equal to the argument name.

Syntax:

public final void setName(String name)
Parameters:
name - the new name for this thread.
Throws:
SecurityException - if the current thread cannot modify this thread.

setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh): java.lang.Thread.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

Syntax:

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
Parameters:
eh - the object to use as the default uncaught exception handler.
If null then there is no
default handler.
Throws:
SecurityException - if a security manager is present and
it denies RuntimePermission ("setDefaultUncaughtExceptionHandler")

setDaemon(boolean on): java.lang.Thread.setDaemon(boolean on) Marks this thread as either a daemon thread or a user thread

Syntax:

public final void setDaemon(boolean on)
Parameters:
on - if true, marks this thread as a daemon thread
Throws:
IllegalThreadStateException - if this thread is alive
SecurityException - if checkAccess() determines that the current
thread cannot modify this thread

public void setContextClassLoader(ClassLoader cl)
Parameters:
cl - the context ClassLoader for this Thread, or null indicating the
system class loader (or, failing that, the bootstrap class loader)
Throws:
SecurityException - if the current thread cannot set the
context ClassLoader

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.