标签:
创建一个类继承Thread
public class RaceCar extends Thread{
private int finish;
private String name;
public RaceCar(String name,int finish){
this.finish = finish;
this.name = name;
}
public void run(){
for(int i = 1;i<=this.finish;i++){
System.out.println(this.name+":"+i);
int num = (int)(Math.random()*6)*1000;
try {
Thread.sleep(num);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(this.name+"finished");
}
}
再创一个带有main方法的类
public class Race {
public static void main(String[] args) {
// TODO Auto-generated method stub
RaceCar []allCars =new RaceCar[5];
for(int i = 0;i<allCars.length;i++){
allCars[i] = new RaceCar(args[i], Integer.parseInt(args[5]));
}
for(RaceCar car:allCars){
car.start();
}
}
}
线程练习完成
标签:
原文地址:http://www.cnblogs.com/whj1986556646/p/5559217.html