实现Runnable 接口

  public class RunableTest {
       public static void main(String[] args) {
           Person person = new Person();
           Thread thread = new Thread(person);
           thread.start();
       }
  }

class Person implements Runnable{

        @Override
        public void run() {
            int times = 0;
            while (true){
                System.out.println("喵喵, 我是小猫咪" + (++times) + " 线程名=" + Thread.currentThread().getName());
                try {
                    // 让该线程休眠 1 秒
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if(times == 80){
                    break; //当 times 到 80, 退出 while, 这时线程也就退出
                }
            }
        }
}
posted @ 2024-05-20 22:57  jock_javaEE  阅读(16)  评论(0)    收藏  举报