Java Thread Class Methods

The Thread class in Java provides a powerful mechanism for managing threads in an application. It allows developers to create, manage, and control the behavior of threads, making it essential for concurrent programming in Java.

This guide covers various methods available in the Thread class. Each method is described in simple terms to help beginners understand how to use them. These methods allow you to perform operations like starting a thread, checking its status, and managing its execution, making it a valuable tool for multithreading and concurrency in Java.

For more detailed information, please refer to the official Java SE Documentation

Java Thread Class Methods

The table below contains frequently used methods of the Java Thread class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
activeCount() Returns the number of active threads in the current thread's thread group.
currentThread() Returns a reference to the currently executing thread object.
getName() Returns the name of the thread.
getPriority() Returns the priority of the thread.
getStackTrace() Returns an array of stack trace elements representing the stack dump of the thread.
getState() Returns the state of the thread.
getThreadGroup() Returns the thread group to which this thread belongs.
interrupt() Interrupts this thread.
interrupted() Tests whether the current thread has been interrupted.
isAlive() Tests if this thread is alive.
isInterrupted() Tests whether this thread has been interrupted.
isDaemon() Tests if this thread is a daemon thread.
join() Waits for this thread to die.
ofPlatform() Creates a new platform 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.
setDaemon() Marks this thread as either a daemon thread or a user thread.
setName() Changes the name of this thread to be equal to the argument name.
setPriority() Changes the priority of this thread.
sleep() Causes the currently executing thread to sleep for the specified number of milliseconds.
start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
threadId() Returns the identifier of this Thread.
yield() Causes the currently executing thread object to temporarily pause and allow other threads to execute.

References:

Comments