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