The Thread
class in Java provides methods to create and manage threads. Threads are a fundamental part of Java’s concurrency model, allowing multiple tasks to be executed in parallel within a single program.
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.
Java Thread Methods
The table below contains various 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 the thread belongs. |
interrupt() | Interrupts the thread. |
interrupted() | Tests whether the current thread has been interrupted. |
isAlive() | Tests if the thread is alive. |
isInterrupted() | Tests whether the thread has been interrupted. |
isDaemon() | Tests if the thread is a daemon thread. |
join() | Waits for the thread to die. |
ofPlatform() | Returns a new Thread object that is a platform thread. |
run() | Starts the execution of the thread. |
setDaemon() | Marks the thread as either a daemon thread or a user thread. |
setName() | Changes the name of the thread. |
setPriority() | Changes the priority of the thread. |
sleep() | Causes the currently executing thread to sleep for a specified number of milliseconds. |
start() | Causes the thread to begin execution. |
threadId() | Returns the identifier of the thread. |
yield() | Causes the currently executing thread object to temporarily pause and allow other threads to execute. |
The Thread
class is part of the java.lang
package. It provides essential methods for creating and managing threads, making it easier to handle concurrency in Java programming.
For more detailed information, refer to the official Java SE Documentation.