gpt4 book ai didi

java - 如何让一个线程做一些工作,然后让另一个线程加入并完成工作?

转载 作者:行者123 更新时间:2023-12-01 13:45:26 26 4
gpt4 key购买 nike

我目前正在尝试学习线程的使用,我发现了一个关于我正在尝试编码的线程的练习。

这是练习:

  1. create an application which simulates the running race of 400 meters.

  2. create five thread groups and give names (Country names).

  3. The number of runners should be then(two in each group) and give names to each runner thread.

  4. 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.

  5. print the winner group name() and all the threads should complete the race.

  6. 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 米(本应在比赛进行到一半时停止。)

最佳答案

您的代码现在正在做什么:

  • 一名运行者跑到 20 点。
  • 然后他启动下一个线程,join() 让他等待,直到新启动的线程结束。
  • 第二个线程现在执行 for 循环,但因为它是一个新的例如,它也从 1 开始一直到 40。
  • 然后就完成了,第一个线程继续执行(其中位于 for 循环的第 20 次迭代中)。

所以最终你得到了 20-40-20 的运行。

您需要确保第二个运行者不会从一个开始,并且第一个运行者在调用第二个运行者后完成。

并且您使用 isGroupRacerWinner(distance) 检查团队是否获胜,您不需要进行每次迭代。如果您在 for 循环之后检查一次就足够了。因为只有完成 for 循环,团队才有机会获胜。

关于java - 如何让一个线程做一些工作,然后让另一个线程加入并完成工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com