java 静态线程_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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值