How to get current executing thread in java

In this tutorial you will learn how to get the current executing thread in java. To get the currently executing thread, you can use the static currentThread() method of the Thread class. This method returns a reference of the currently executing thread object.

Below is the example code of getting current executing thread in java

public class CurrentThreadExample {
    public static void main(String[] args) {
        // Get the currently executing thread object
        Thread thread = Thread.currentThread();
        System.out.println("Thread id       : " + thread.getId());
        System.out.println("Thread name     : " + thread.getName());
        System.out.println("Thread priority : " + thread.getPriority());
        System.out.println("Total thread    : " + Thread.activeCount());

The output of above example will be as:

Thread id       : 1
Thread name     : main
Thread priority : 5
Total thread    : 1