扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java怎么获取当前线程名称

扬州沐宇科技
2023-10-28 03:23:02
Java

可以通过Thread类的getName()方法来获取当前线程的名称。

示例代码如下:

public class Main {
    public static void main(String[] args) {
        Thread currentThread = Thread.currentThread();
        String threadName = currentThread.getName();
        System.out.println("当前线程名称:" + threadName);
    }
}

运行结果:

当前线程名称:main

扫码添加客服微信