package com.thread;
/**
* 2010-10-26
* 通过继承Thread实现线程
* 每秒循环打印hello world
*/
public class Demo1 {
public static void main(String[] args) {
Cat cat=new Cat();
//启动线程,让run函数运行
cat.start();
}
}
class Cat extends Thread{
//重写run函数
public void run(){
while(true){
try {
//休眠1秒
//1000代表1000毫秒
//sleep会让线程进入到阻塞状态(Blocked),并且释放资源
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello,world");
}
}
}
线程示例【1】
本文介绍了一个简单的Java线程示例,通过继承Thread类并重写run方法,实现了每秒输出一次hello, world的功能。文章展示了如何创建和启动线程,并使用Thread.sleep方法使线程暂停一秒。

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



