hi danpost
I am doing this program, , thread and it is not giving me any output. here is part of the question.
Create an application which simulate the running race of 400 meters.
create five thread groups and give names(country names).
each thread should run exactly half the distance 200m and the next thread in same group should join the race to complete it
prin the winner group() and all the treads should complete the threads the race
Print the time take by each group to complete the race and highlight the winners time
here is what I have so far
I am not getting any output when i run the program, why is that, how can it be fixed and how can i get the desired result
any help would be greatly appreciated
public class RelayRaceDemo {
public static Thread runner[];
public static void main(String[]args){
RelayRacer rr = new RelayRacer();
ThreadGroup country[] = new ThreadGroup[5];
country[0] = new ThreadGroup("Tinidad and Tobago");
country[1] = new ThreadGroup("Jamiaca");
country[2] = new ThreadGroup("St Vincent");
country[3] = new ThreadGroup("Barbados");
country[4] = new ThreadGroup("Bahamas");
Thread[] runner = new Thread[7];
Runnable relayRacer = null;
runner[0]= new Thread(country[0],relayRacer,"Trinidad racer 1");
runner[1]= new Thread(country[0],relayRacer,"Trinidad racer 2");
runner[2]= new Thread(country[1],relayRacer,"Tobago racer 1");
runner[3]= new Thread(country[1],relayRacer,"Tobago racer 2");
runner[4]= new Thread(country[2],relayRacer,"Bahamas racer 1");
runner[5]= new Thread(country[2],relayRacer,"Barbados racer 1");
runner[6]= new Thread(country[3],relayRacer,"St Vincent racer 1");
runner[0].start();;
runner[2].start();
runner[4].start();
runner[6].start();
}
public void relayRace(){
boolean winnerYet = false;
for(int distance=1; distance <=40; distance++){
System.out.println("Current runner"+ Thread.currentThread().getName() + " Has run "+
distance + "metres");
if(Thread.currentThread().getName().equals("Trinidad and Toabgo racer 1") && distance == 20)
{
threadJoin(distance, 0);
}
else if(Thread.currentThread().getName().equals("Jamiaca racer 1") && distance ==20){
threadJoin(distance, 2);
}
else if(Thread.currentThread().getName().equals("St Vincent racer 1")&& distance == 20){
threadJoin(distance, 2);
}
else if(Thread.currentThread().getName().equals("Barbados racer 1 ") && distance ==20){
threadJoin(distance, 3);
}
else if(Thread.currentThread().getName().equals("Bahamas racer 1") && distance == 20){
threadJoin(distance, 4);
}
if(isGroupRaceWinner (distance)){
System.out.println("Winning Country is"+Thread.currentThread().getThreadGroup());
}
}
}
public void threadJoin(int distance, int nextRunner){
//RelayRacerDemo
}
public boolean isGroupRaceWinner(int distance){
boolean winnerYet = false;
if(distance == 40 && winnerYet == false){
winnerYet = true;
return true;
}
else{
return false;
}
}
@Override
public void run(){
this.relayRace();
}
}

