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