第一步:建立线程类,并启动线程
1 /** 2 * 【1】实现Runnable接口,重写run方法。 3 * 【2】run方法内的逻辑代码就是线程体 4 * 【3】创建线程类的对象 5 * 【4】创建线程类的代理对象 6 * 【5】代理对象调用start(),启动线程 7 * @author it-c-1201 8 * 9 */ 10 public class MyThread implements Runnable{ 11 12 //线程体 13 public void run() { 14 for(int a=0;a<100;a++){ 15 System.out.println("MyThread.run(线程体运行)"+a); 16 } 17 18 } 19 20 21 public static void main(String[] args) { 22 //创建真实角色。线程体本身的对象 23 MyThread myThread=new MyThread(); 24 //创建静态代理角色 25 Thread dailiMyThread=new Thread(myThread); 26 //启动线程 27 dailiMyThread.start(); 28 29 for(int a=0;a<100;a++){ 30 System.out.println("MyThread.main(main方法)"+a); 31 } 32 } 33 34 35 }