标签:中国 tle bsp string 应用 应用程序 独立 范围 笔记
/**
* 演示如何通过继承Thread来开发线程
*/
public class Thread01 {
public static void main(String[] args) {
//创建一个 Cat对象
Cat cat=new Cat();
//启动线程
cat.start();//.start()会导致run函数运行
}
}
class Cat extends Thread{
int times=0;
//重写run函数
public void run(){
while(true){
//休眠一秒
//1000表示1000毫秒
try {
Thread.sleep(1000);//sleep就会让该线程进入到Blocked阻塞状态,并释放资源。
} catch (Exception e) {
e.printStackTrace();
}
times++;
System.out.println("hello,world!"+times);
if(times==10){
//退出线程
break;
}
}
}
}
/**
* 演示如何通过Runnable接口来开发线程
*/
public class Thread02{
public static void main(String []args){
Dog dog=new Dog();
//创建线程
Thread t=new Thread(dog);
//启动线程
t.start();
}
}
class Dog implements Runnable{//创建Runnable接口
public void run(){//重写run函数
int times=0;
while(true){
try{
Thread.sleep(1000);
}catch (Exception e) {
e.printStackTrace();
}
times++;
System.out.println("hello,wrold!"+times);
if(times==10){
break;
}
}
}
}
标签:中国 tle bsp string 应用 应用程序 独立 范围 笔记
原文地址:https://www.cnblogs.com/xuxaut-558/p/10045741.html