Java Thread currentThread() Method
Example
Print the name of the current thread, in main() and in run():
public class Main {
static class MyThread extends Thread {
public void run() {
System.out.println("In run(): " + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
System.out.println("In main(): " + Thread.currentThread().getName());
MyThread thread = new MyThread();
thread.setName("Thread 1");
thread.start();
}
}
Definition and Usage
The currentThread() method returns the Thread object of the thread that is running the code right now.
It is a static method, so you call it on the class: Thread.currentThread(). The thread that runs the main() method is called main.
Syntax
public static Thread currentThread()
Parameter Values
None.
Technical Details
| Returns: | The Thread object of the thread that runs the code. |
|---|
Related Pages
❮ Thread Methods