Java Thread类静态线程 static Thread currentThread()方法(带示例)
线程类静态线程currentThread()
包java.lang.Thread.currentThread()中提供了此方法。
此方法用于返回当前正在执行的线程对象的引用。
此方法是静态的,因此也可以使用类名访问此方法。
该方法的返回类型为Thread,它返回当前正在执行的线程对象的引用。
此方法不会引发任何异常。
语法:
static Thread currentThread(){
}参数:
我们不会在File方法中将任何对象作为参数传递。
返回值:
该方法的返回类型为Thread,它返回当前执行线程的引用。
Java程序演示currentThread()方法示例
/*
我们将使用Thread类方法,因此我们将导入
包,但不是强制性的,因为
默认情况下导入
*/
import java.lang.Thread;
class Thread1 extends Thread {
//覆盖run()Thread类
public void run() {
/* 显示当前执行线程的线程名。使用Thread.currentThread().getName()
*/
System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
}
}
class Thread2 extends Thread {
public void run() {
/* 显示当前执行线程的线程名,使用 Thread.currentThread () . getName ()
*/
System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
}
}
public class MainThread {
public static void main(String[] args) {
/* 显示当前执行线程的线程名,使用 Thread.currentThread () . getName ()
*/
System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
//创建Thread1对象
Thread1 t1 = new Thread1();
//通过使用start()Thread类start()将调用和
//它将调用run()Thread1类
t1.start();
Thread2 t2 = new Thread2();
//通过使用start()Thread类start()将调用
//它将调用run()Thread2类
t2.start();
}
}输出结果
E:\Programs>javac MainThread.java E:\Programs>java MainThread The name of this thread is main The name of this thread is Thread-0 The name of this thread is Thread-1
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短