在Java中获取当前线程的名称
可以使用以下代码获取当前线程的名称-
示例
import java.io.*;
class name_a_thread extends Thread {
@Override
public void run() {
System.out.println("Fetching the name of the current thread-");
System.out.println(Thread.currentThread().getName());
}
}
public class Demo {
public static void main (String[] args) {
name_a_thread thr_1 = new name_a_thread();
name_a_thread thr_2 = new name_a_thread();
thr_1.start();
thr_2.start();
}
}输出结果
Fetching the name of the current thread- Thread-0 Fetching the name of the current thread- Thread-1
名为“name_a_thread”的类扩展到父类Thread。它会覆盖“运行”方法,并且在调用此函数时,控制台上会显示相关消息。名为Demo的类包含主要功能,其中创建了该类的两个实例。这两个实例都使用“开始”功能调用。
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短