线程(继承: 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 @   霍叔  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示