/**
* 这是一个计数器线程
*/
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(); //启动线程
}
}