线程(实现接口:implements Runnable)

发布时间 2023-03-30 15:56:56作者: 霍叔
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;
}
}


}
}