首页 > 编程语言 > 详细

线程练习

时间:2016-06-04 19:15:59      阅读:118      评论:0      收藏:0      [点我收藏+]

创建一个类继承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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!