首页 > 基础教程 > 多线程 > Thread类
Java Thread.currentThread()获取当前线程
Thread.currentThread()是Thread的一个静态方法,用来获取当前线程对象的一个引用。
例子
public class Demo {
public static void main(String[] args) {
MyThread mt = new MyThread();
new Thread(mt).start();
new Thread(mt, "Name1").start();
new Thread(mt, "Name2").start();
System.out.println(Thread.currentThread().getName()); // main主方法
// System.out.println(this.getName()); // this获取不到线程对象
}
}
class MyThread extends Thread {@Override public void run() {
try {
Thread.sleep(3000);
Thread t = Thread.currentThread();
System.out.println("当前线程名字:" + t.getName() + " 当前线程的优先级别为:" + t.getPriority() + " ID:" + t.getId());</