线程(继承: extends Thread)

public class test1 {

@SuppressWarnings({"all"})
public static void main(String[] args) throws InterruptedException {


Cat cat = new Cat();
cat.start();//启动线程
// cat.run();//这种是运行静态方法,之后当run方法执行完成后才会继续往下执行 不推荐

for (int i = 0; i < 60; i++) {
System.out.println("主线程 i="+ i);
Thread.sleep(1000);
}


}


}

//当一个类继承了Thread 就会被当成线程使用
class Cat extends Thread{
int times=0;

@Override
public void run(){

while (true){

System.out.println("你好我是小猫咪"+(++times) + "线程名:"+ Thread.currentThread().getName());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}


if (times>80){
break;
}

}

}

}
posted @ 2023-03-30 14:21  霍叔  阅读(43)  评论(0)    收藏  举报