Java Thread currentThread()方法

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

1 语法

public static Thread currentThread()

2 参数

3 返回值

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

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.currentThread()方法的例子
 */
import java.lang.*;

public class Demo implements Runnable {

    Demo() {
        // 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 Demo();
    }
}

输出结果为:

current thread = Thread[main,5,main]
thread created = Thread[Admin Thread,5,main]
This is run() method

热门文章

优秀文章