- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试学习线程的使用,我发现了一个关于我正在尝试编码的线程的练习。
这是练习:
create an application which simulates the running race of 400 meters.
create five thread groups and give names (Country names).
The number of runners should be then(two in each group) and give names to each runner thread.
Each thread should run exactly half the distance - 200 m and the next thread in the same group should join the race to complete it.
print the winner group name() and all the threads should complete the race.
Print the time taken by each Group to complete the race and highlight the winners time.
所以,我的问题是关于第四点,每个线程应该跑一半的距离,我怎样才能让我的初始线程完成并让下一个线程替换它并完成比赛?
这是我到目前为止的代码:
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-20 米的一半比赛) 40 米。)第二名选手完成比赛后,第一名选手继续比赛,并从 20 到 40 跑完剩余的 20 米(本应在比赛进行到一半时停止。)
最佳答案
您的代码现在正在做什么:
join()
让他等待,直到新启动的线程结束。所以最终你得到了 20-40-20 的运行。
您需要确保第二个运行者不会从一个开始,并且第一个运行者在调用第二个运行者后完成。
并且您使用 isGroupRacerWinner(distance)
检查团队是否获胜,您不需要进行每次迭代。如果您在 for
循环之后检查一次就足够了。因为只有完成 for
循环,团队才有机会获胜。
关于java - 如何让一个线程做一些工作,然后让另一个线程加入并完成工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402683/
我是一名优秀的程序员,十分优秀!