package threaduse; /** * Copyright (C), 2018-2021, Mr.Lin * Author: Mr.Lin * Date: 2021/11/27 0:57 * FileName: Thread01 * Description: 演示通过thread类创建线程 */ public class Thread01 { public static void main(String[] args) { Cat cat = new Cat(); cat.start(); } } //1.当一个类继承了Thread类该类就可以当作线程使用 class Cat extends Thread { @Override public void run() {//重写run方法写上自己的逻辑 int times=0; while (true){ System.out.println("喵喵喵,我是猫咪"+(++times)); //休眠一秒 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if(times==8){ System.out.println("退出线程"); break; } } } }