How do I get the currently executing thread?

To get the currently executing thread, use the static currentThread() method of the Thread class. This method returns a reference of the currently executing thread object.

package org.kodejava.example.lang;

public class GetCurrentThreadDemo {
    public static void main(String[] args){
        //
        // Get the currently executing thread object
        //
        Thread thread = Thread.currentThread();
        System.out.println("Id      : " + thread.getId());
        System.out.println("Name    : " + thread.getName());
        System.out.println("Priority: " + thread.getPriority());
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply