Advertisement
heavenriver

Judge.java (judge version)

Apr 15th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. package runners_judge;
  2.  
  3. import java.util.*;
  4.  
  5. public class Judge implements Runnable
  6.  
  7.     {
  8.     private static ArrayList<Runner> chart = new ArrayList<Runner>();
  9.    
  10.     public static synchronized void addRunner(Runner r)
  11.         {
  12.         if(r.hasArrived())
  13.             chart.add(r);
  14.         if(chart.size() == 1)
  15.             System.out.println(r.toString() + " won the competition!");
  16.         }
  17.    
  18.     public static synchronized void printChart()
  19.         {
  20.         for(int i = 0; i < chart.size(); i++)
  21.             System.out.println(chart.get(i).toString() + " has reached position " + i);
  22.         }
  23.    
  24.     public void run()
  25.         {
  26.         synchronized(this)
  27.             {
  28.             while(chart.isEmpty())
  29.                 {
  30.                 try
  31.                     {
  32.                     wait();
  33.                     }
  34.                 catch(InterruptedException e)
  35.                     {
  36.                     e.printStackTrace();
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement