Java.lang.Thread.currentThread() 方法
描述
java.lang.Thread.currentThread() 方法返回对当前正在执行的线程对象的引用。
声明
以下是 java.lang.Thread.currentThread() 方法的声明。
public static Thread currentThread()
参数
NA
返回值
该方法返回当前正在执行的线程。
异常
NA
示例
下面的例子展示了 java.lang.Thread.currentThread() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ThreadDemo implements Runnable {
ThreadDemo() {
// main thread
Thread currThread = Thread.currentThread();
// thread created
Thread t = new Thread(this, "Admin Thread");
System.out.println("current thread = " + currThread);
System.out.println("thread created = " + t);
// this will call run() function
t.start();
}
public void run() {
System.out.println("This is run() method");
}
public static void main(String args[]) {
new ThreadDemo();
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
current thread = Thread[main,5,main] thread created = Thread[Admin Thread,5,main] This is run() method

