This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/5/12

why am i not getting any output from this thread.

divinity divinity

2017/5/12

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

2017/5/12

#
Remove
Thread[]
from the beginning of line 17.
divinity divinity

2017/5/12

#
am i supposed to removed it. and what is the problem with that
divinity divinity

2017/5/12

#
I just comment out line 17 and run the code and this is the result i got
Exception in thread "main" java.lang.NullPointerException
	at RelayRaceDemo.main(RelayRaceDemo.java:29)
Java Result: 1
danpost danpost

2017/5/12

#
Do not remove the entire line -- just the beginning of it (just what I had shown). You do not want to assign the array to a different variable than the global field set up on line 3. FYI, the code as given has two distinct arrays with the same name -- the global one set up on line 3 and the method local one you are (were) setting up on line 17. The removal of the type on line 17 will force 'runner' in the line (and the following lines) be the one set up on line 3 (instead of 'runner' being a new local variable for the rest of the method).
divinity divinity

2017/5/12

#
hi danpost i removed it and this is the result i got
run:
BUILD SUCCESSFUL (total time: 0 seconds)
got no output when i remove the part u told me to remove
danpost danpost

2017/5/13

#
Well, it was obvious to me that it should be removed or line 3 should be removed. After further review, I can see how it does not change matters. I noticed that you have non-static methods in the class given. These can only be executed on objects of that class, RelayRaceDemo objects. No objects of this class are being created, however (and might not need to be -- maybe those methods should be 'static' as well). The 'run' method should belong to a Thread (or Runnable) object (from what I understand). Unfortunately, I am not that familiar with Threads enough to help you more specifically.
You need to login to post a reply.