class Do3 { public static void main(String[] args) { XC d1=new XC("小王"); XC d2=new XC("xiaoli"); d1.start();//启动线程,调用run()方法 d2.start(); System.out.println("Hello World!"); } } class XC extends Thread//继承线程 { private String name; XC(String name) { this.name=name; } public void run()//重写run()方法 { for(int x=0;x<10;x++) { for(int y=-999999;y<999999;y++){} System.out.println(name+"....="+x); } } }
第二种方法
class Do6 { public static void main(String[] args) { Yunxing yx1=new Yunxing("效力"); Yunxing yx2=new Yunxing("dddd"); Thread d1=new Thread(yx1); Thread d2=new Thread(yx2); d1.start(); d2.start(); } } class Yunxing implements Runnable//此处用接口形式的线程 ,可以继承父类 { private String name; Yunxing(String name) { this.name=name; } public void run() { for (int x=0;x<10 ;x++ ) { for(int y=-999999;y<999999;y++) { }System.out.println(name+".."+x); } } }