currentThread 方法打印出不同的线程名字
Thread.currentThread() 的作用是获取当前的线程 如下的代码 演示了currentThread 方法打印出不同的线程名字.
/**
* 类名称:CurrentThread
* 类描述: 演示打印main, Thread-0, Thread-1
*
* @author: https://javaweixin6.blog.csdn.net/
* 创建时间:2020/8/30 15:32
* Version 1.0
*/
public class CurrentThread implements Runnable {
public static void main(String[] args) {
new CurrentThread().run();
new Thread(new CurrentThread()).start();
new Thread(new CurrentThread()).start();
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
控制台打印如下, 可以看到 . 第一行打印为main线程的调用了run方法. 第二行打印为第一个子线程调用了run方法 第三行打印为第二个子线程调用了run方法