Java Thread currentThread()方法

Thread类的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

上一篇: Java Runtime类 下一篇:无