求大神详细解释每一行代码的意思和作用

2025-04-06 11:09:47
推荐回答(1个)
回答1:

/**
* 这是一个计数器线程
*/
public class TimerThread extends Thread { //继承Tread类,TimerThread就是一个线程类
int n= 0; //初始计数为0
public void run(){
while(true){ //死循环
System.out.println(n); //打印计数
n++; //计数器自增长
try{
sleep(1000); //延时1秒钟
}
catch(InterruptedException e){
return;
}
}
}
public static void main(String[] args) {
TimerThread th = new TimerThread();
th.start(); //启动线程
}
}