【发布时间】:2018-12-05 02:56:17
【问题描述】:
所以在尝试完成我的硬件时,还有一件事对我来说并不顺利。 在这个程序中,我使用了 2 个类,这是运行的压缩, 我只是想宣布赢得比赛的玩家的位置。
例如:我们有 4 个线程。 如果第二个线程最后达到100M,我想发布一个他排在第4位的MSG。
赛车手级别:
package assig1_2;
public class Racer extends Thread{
public static int globalid = 1;
private int id;
private int speed;
private Track track;
public Racer(int Speed,Track track) {
this.speed = Speed;
this.track = track;
this.id = this.globalid;
this.globalid++;
}
public void run() {
go ();
}
void go () {
this.track.setPriority(this.speed);
for(int i=1; i<=10; i++) {
System.out.println("runner " + this.id + " run " + i + " meters " );
if (i == 10) {
this.track.setPlace(id);
System.out.println("Runner " + this.id + " finished " + track.getPlaceNumber() + track.getPlaceName() );
}
}
}
}
轨道类别:
package assig1_2;
public class Track extends Thread {
private int finishedRacers;
public String place;
public void setPlace(int numOfPlace) {
if (numOfPlace == 1) {
this.finishedRacers = numOfPlace;
this.place = "st";
} else if (numOfPlace == 2) {
this.finishedRacers = numOfPlace;
this.place = "nd";
} else if (numOfPlace == 3) {
this.finishedRacers = numOfPlace;
this.place = "rd";
} else {
this.finishedRacers = numOfPlace;
this.place = "th";
}
}
public int getPlaceNumber() {
return this.finishedRacers;
}
public String getPlaceName() {
return this.place;
}
}
当我们使用 Thread 时,它在我们第一课中的愚蠢程序。
【问题讨论】:
标签: java multithreading oop