目录

static Thread currentThread()

描述 (Description)

java.lang.Thread.currentThread()方法返回对当前正在执行的线程对象的引用。

声明 (Declaration)

以下是java.lang.Thread.currentThread()方法的声明

public static Thread currentThread()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回当前正在执行的线程。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.Thread.currentThread()方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018