Java如何获取当前正在执行的线程?

要获取当前正在执行的线程,请使用 Thread 类的静态currentThread()方法。此方法返回当前正在执行的线程对象的引用。

package org.nhooo.example.lang;

public class GetCurrentThreadDemo {
    public static void main(String[] args){
        // 获取当前正在执行的线程对象
        Thread thread = Thread.currentThread();
        System.out.println("Id      : " + thread.getId());
        System.out.println("Name    : " + thread.getName());
        System.out.println("Priority: " + thread.getPriority());
    }
}

该代码段显示以下输出:

Id      : 1
Name    : main
Priority: 5