Java Thread Methods
Common Thread Methods
A thread is a part of a program that can run at the same time as other parts. The Thread class has methods to start, pause, name and control threads.
The Thread class is in the java.lang package, so you do not need to import it. The table below lists the most common methods.
| Method | Description | Return Type |
|---|---|---|
| currentThread() | Returns the thread that is running the current code | Thread |
| getName() | Returns the name of the thread | String |
| getPriority() | Returns the priority of the thread, a number from 1 to 10 | int |
| interrupt() | Interrupts the thread, for example to stop it from sleeping or waiting | void |
| isAlive() | Checks if the thread has started and is not finished yet | boolean |
| isInterrupted() | Checks if the thread has been interrupted | boolean |
| join() | Waits for the thread to finish | void |
| run() | Contains the code that the thread runs | void |
| setName() | Changes the name of the thread | void |
| setPriority() | Changes the priority of the thread, a number from 1 to 10 | void |
| sleep() | Pauses the current thread for the specified number of milliseconds | void |
| start() | Starts the thread, so it runs the code in its run() method | void |