多线程实现方式1-继承Thread类

public class Demo1_Thread {

  public static void main(String[] args) throws InterruptedException {
    Demo demo = new Demo(); //4.创建Thread类的子类对象
    demo.start(); //5.开启线程

    for(int i = 0; i<10; i++) {
      System.out.println("BBBBBBBBBBBBBBBBBBBB");
      Thread.sleep(100);
    }
  }
}

class Demo extends Thread{ //1.继承Thread类
  public void run() { //2.重写run方法
    for (int i = 0; i < 10; i++) { //3.将要执行的代码写在run方法内
      System.out.println("AAAAAAAAAAAAAAAAAAAAAA");
      try {
      sleep(100);
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
    }
  }
}

posted @   幻音坊-女帝  阅读(214)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示