线程(实现接口:implements Runnable)

//Runnable接口更适合多线程共用一个资源的情况,并且避免了单继承的限制
public class test02 {


public static void main(String[] args) {

Dog dog = new Dog();
Thread thread = new Thread(dog);
thread.start();

}
}


class Dog implements Runnable {

int count = 0;

@Override
public void run() {//通过实践Runnable接口来实现线程


while (true) {
System.out.println("Hi:" + (++count) + " 线程名称= " + Thread.currentThread().getName());


try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (count == 10) {
break;
}
}


}
}
posted @ 2023-03-30 15:51  霍叔  阅读(70)  评论(0编辑  收藏  举报