hi all

I am doing this thread program and when i run it is not giving any output, can someone tell me why no output is shown.

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");

         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   class RelayRacer implements Runnable{

   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();
            }

}

Recommended Answers

All 3 Replies

Your threads are created with a Runnable that is null.

It should be RelayRace implements Runnable, not RelayRacer. Or in the immortal words of Captain Kidd - ARR!

I agree that the naming is a bit confusing, but the code for RelayRacer looks valid to me as it is. Just changing the name won't fix anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.