packagewww.theone.threaddemo;//创建线程方法一 实现Runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法publicclassDemo01implementsRunnable{@Override//run方法线程体publicvoidrun(){for(int i =0; i <100; i++){System.out.println("次线程:"+ i);}}publicstaticvoidmain(String[] args){//创建Runnable接口实现类的对象Demo01 demo01 =newDemo01();newThread(demo01).start();//Main主线程for(int i =0; i <1000; i++){System.out.println("主线程:"+i);}}}
方法二、通过继承Thread类
packagewww.theone.threaddemo;//创建线程方法二 继承Thread类, 重写run方法, 调用start方法开启线程publicclassDemo02extendsThread{@Overridepublicvoidrun(){for(int i =0; i <100; i++){System.out.println("学习多线程"+i);}}publicstaticvoidmain(String[] args){//创建继承Thread类的类对象Demo02 demo02 =newDemo02();
demo02.start();for(int i =0; i <1000; i++){System.out.println("主线程"+i);}}}