可以通过Thread类的getName()方法来获取当前线程的名称。
示例代码如下:
public class Main {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
String threadName = currentThread.getName();
System.out.println("当前线程名称:" + threadName);
}
}
运行结果:
当前线程名称:main
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java怎么获取当前线程