admin管理员组

文章数量:1612099

currentThread 方法打印出不同的线程名字

Thread.currentThread() 的作用是获取当前的线程
如下的代码 演示了currentThread 方法打印出不同的线程名字.

/**
 * 类名称:CurrentThread
 * 类描述: 演示打印main, Thread-0, Thread-1
 *
 * @author: https://javaweixin6.blog.csdn/
 * 创建时间: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方法

本文标签: 线程多线程名字方法Java