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 get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024