package com.thread;
/**
* 2010-10-26
* 继承Thread实现线程
* 每秒打印hello world,当打印10次时退出打印
* @author Administrator
*
*/
public class Demo2 {
public static void main(String args[]){
Dog dog=new Dog();
dog.start();
}
}
class Dog extends Thread{
public void run(){
int time=0;
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello,world");
time++;
if(time==10){
//退出,跳出while循环,线程也就死亡
break;
}
}
}
}
线程示例【2】
本文提供了一个使用Java实现线程的简单示例。通过继承Thread类并覆盖run方法,创建了一个名为Dog的线程,该线程每秒钟打印一次hello, world,并在打印10次后自动停止。

被折叠的 条评论
为什么被折叠?



