码迷,mamicode.com
首页 > 编程语言 > 详细

线程练习

时间:2016-06-04 19:19:05      阅读:165      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!