【发布时间】:2013-12-22 13:14:51
【问题描述】:
我目前正在尝试学习线程的使用,我发现了一个关于我正在尝试编码的线程的练习。
这是练习:
创建一个模拟 400 米赛跑的应用程序。
创建五个线程组并给出名称(国家名称)。
跑步者的数量应该是(每组两个),并为每个跑步者线程命名。
每个线程应该跑完一半的距离 - 200 m,并且同一组中的下一个线程应该加入比赛以完成它。
打印获胜者组名(),所有线程都应该完成比赛。
打印每个组完成比赛所用的时间并突出显示获胜者的时间。
所以,我的问题是关于每个线程应该跑一半距离的第 4 点,我怎样才能让我的初始线程完成并让下一个线程替换它并完成比赛?
这是我目前的代码:
public class RelayRacerDemo {
public static Thread runner[];
public static void main(String[] args) {
RelayRacer rRacer = new RelayRacer();
ThreadGroup country[] = new ThreadGroup[5];
country[0] = new ThreadGroup("USA");
country[1] = new ThreadGroup("Mexico");
country[2] = new ThreadGroup("Italia");
country[3] = new ThreadGroup("France");
country[4] = new ThreadGroup("Brazil");
runner = new Thread[10];
runner[0] = new Thread(country[0] , rRacer, "USA racer 1");
runner[1] = new Thread(country[0] , rRacer, "USA racer 2");
runner[2] = new Thread(country[1] , rRacer, "Mexico racer 1");
runner[3] = new Thread(country[1] , rRacer, "Mexico racer 2");
runner[4] = new Thread(country[2] , rRacer, "Italia racer 1");
runner[5] = new Thread(country[2] , rRacer, "Italia racer 2");
runner[6] = new Thread(country[3] , rRacer, "France racer 1");
runner[7] = new Thread(country[3] , rRacer, "France racer 2");
runner[8] = new Thread(country[4] , rRacer, "Brazil racer 1");
runner[9] = new Thread(country[4] , rRacer, "Brazil racer 2");
runner[0].start();
runner[2].start();
runner[4].start();
runner[6].start();
runner[8].start();
}
}
还有我的可运行代码:
public class RelayRacer implements Runnable{
public static Boolean winnerYet = false;
public void relayRace(){
for(int distance=1; distance<=40; distance++){
System.out.println("current runner " + Thread.currentThread().getName()
+ " has run " + distance + " meters");
if(Thread.currentThread().getName().equals("USA racer 1") && distance == 20){
threadJoin(distance, 1);
}else if(Thread.currentThread().getName().equals("Mexico racer 1") && distance == 20){
threadJoin(distance, 3);
}else if(Thread.currentThread().getName().equals("Italia racer 1") && distance == 20){
threadJoin(distance, 5);
}else if(Thread.currentThread().getName().equals("France racer 1") && distance == 20){
threadJoin(distance, 7);
}else if(Thread.currentThread().getName().equals("Brazil racer 1") && distance == 20){
threadJoin(distance, 9);
}
if(isGroupRacerWinner(distance)){
System.out.println("Winning Country is " + Thread.currentThread().getThreadGroup().getName());
}
}
}
public void threadJoin(int distance, int nextRunner){
RelayRacerDemo.runner[nextRunner].start();
try
{RelayRacerDemo.runner[nextRunner].join();
}catch (InterruptedException e) {e.printStackTrace();}
}
public Boolean isGroupRacerWinner(int distance){
if(distance == 40 && winnerYet == false){
winnerYet = true;
return true;
}else
return false;
}
@Override
public void run(){
this.relayRace();
}
}
我把它改成了 40 米,这样我可以更容易地调试我的代码,
runner = thread;
是这样的:每个国家的第一个跑者跑完前 20 米,然后第二个跑者加入比赛,跑完整个比赛(全部 40 米,当它应该只跑 20- 的一半时) 40米。)第二个跑者跑完后,第一个跑者继续比赛,从20-40跑剩下的20米,(应该在半场停下的时候。)
【问题讨论】:
-
如果您需要通过
Thread获取计算结果,则不需要使用Thread.join()。你也可以use afuture。
标签: java multithreading