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.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());
}
}
The code snippet print the following output:
Id : 1
Name : main
Priority: 5
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023