Java教程

Java currentThread()方法

线程类的 currentThread()方法用于返回对当前正在执行的线程对象的引用。

语法

public static Thread currentThread()

返回值

它返回当前正在执行的线程。

示例

public class CurrentThreadExp extends Thread
{
    public void run()
    {
        // print currently executing thread
        System.out.println(Thread.currentThread().getName());
    }
    public static void main(String args[])
    {
        // creating two thread
        CurrentThreadExp t1=new CurrentThreadExp();
        CurrentThreadExp t2=new CurrentThreadExp();
        // this will call the run() method
        t1.start();
        t2.start();
    }
}
输出:
Thread-0
Thread-1

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4