java.lang.Thread.currentThread()方法实例
java.lang.Thread.currentThread() 方法返回一个引用到当前正在执行的线程对象。
声明
以下是java.lang.Thread.currentThread()方法的声明
public static Thread currentThread()
参数
-
NA
返回值
此方法返回当前正在执行的线程。
异常
-
NA
例子
下面的例子显示java.lang.Thread.currentThread()方法的使用。
package com.yiibai; 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