Java Get Current Thread Name and ID

Tags: Thread

In this Java tutorial, we learn how to get currently executing thread name and ID in Java programming language.

How to get name and ID of current executing thread in Java

In Java, to get the currently executing Thread object we can use the Thread.currentThread() static method, and then call the getName() and getId() method on that return Thread object to get the thread name and ID as the following example Java program.

GetCurrentThreadNameExample.java

public class GetCurrentThreadNameExample {
    public static void main(String... args) {
        // Get Current Thread Name
        String threadName = Thread.currentThread().getName();
        // Get Current Thread ID
        long threadId = Thread.currentThread().getId();

        System.out.println("Current Thread Name: " + threadName);
        System.out.println("Current Thread ID: " + threadId);
    }
}
The output as below.
Current Thread Name: main
Current Thread ID: 1

Happy Coding 😊

Java Get Local Host IP Address

Java Get Local Host Name