线程练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程练习相关的知识,希望对你有一定的参考价值。
创建一个类继承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();
}
}
}
线程练习完成
以上是关于线程练习的主要内容,如果未能解决你的问题,请参考以下文章