| class ThreadDemo1 { 
 public static void main(String[] args) {
 MyThread mt = new MyThread();        // 4.创建线程对象
 mt.start();                        // 5.开启新线程, 内部会自动执行run方法
 
 for (int i = 0; i < 1000; i++)
 System.out.println("A");
 }
 
 }
 
 class MyThread extends Thread {                        // 1.定义类继承Thread
 public void run() {                        // 2.重写run方法
 for (int i = 0; i < 1000; i++)        // 3.把新线程要做的事写在run方法中
 System.out.println("B");
 }
 }
 |