有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

以下是练习:

  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-40米的比赛中,第二名选手只能跑一半)第二名选手完成比赛后,第一名选手继续比赛,从20-40跑完剩下的20米(本应在比赛进行到一半时停止)


共 (1) 个答案

  1. # 1 楼答案

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

    • 一名跑步者跑到20岁
    • 然后他开始下一个线程,让我们等到 新开始的线程完成
    • 第二个线程现在执行for循环,但它是一个新线程 例如,它也从1开始,一直持续到40
    • 然后它完成,第一个线程继续执行(这 在for循环的第20次迭代中)

    所以最后你会得到你的20-40-20分

    你需要确保第二名选手不是从一点开始,第一名选手在呼叫第二名选手后结束比赛

    检查一个团队是否以isGroupRacerWinner(distance)获胜,你不需要做每一次迭代。在for循环之后检查一次就足够了。因为球队只有完成for循环才有机会获胜