Facebook
From Smelly Zebra, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 317
  1. import java.io.Reader;
  2. import java.util.Scanner;
  3. import java.util.concurrent.ConcurrentHashMap;
  4.  
  5. public class Relay {
  6.     private Integer nextId;
  7.     private Integer currentId;
  8.     private ConcurrentHashMap<Thread,Integer> idMap;
  9.     private Scanner data;
  10.     private Thread T;
  11.     private boolean koniec;
  12.     private boolean start;
  13.  
  14.     public synchronized void inform(){
  15.         notifyAll();
  16.     }
  17.  
  18.     public Relay(Reader reader) {
  19.         data=new Scanner(reader);
  20.         idMap=new ConcurrentHashMap<Thread,Integer>();
  21.         T= new Thread(){
  22.             public void run(){
  23.                 while(data.hasNextInt()){
  24.                     synchronized (Relay.this){
  25.                          if(nextId==null) {
  26.                                 nextId = data.nextInt();
  27.                                 inform();
  28.                         }
  29.                         try {
  30.                             wait();
  31.                         }
  32.                         catch (InterruptedException e) {
  33.                         }
  34.                     }
  35.                 }
  36.                 koniec=true;
  37.                 inform();
  38.             }
  39.         };
  40.         T.start();
  41.  
  42.     }
  43.  
  44.  
  45.     public synchronized void register(int id, Thread competitor) {
  46.         idMap.put(competitor,id);
  47.     }
  48.  
  49.     public synchronized void startRelayRace() {
  50.         start=true;
  51.         inform();
  52.     }
  53.  
  54.     public synchronized boolean dispatch() {
  55.         if(koniec && nextId==null && currentId==null) {
  56.             try {
  57.                 return false;
  58.             }
  59.             finally {
  60.                 inform();
  61.             }
  62.         }
  63.  
  64.         while(!start){
  65.             try{
  66.                 wait();
  67.             } catch (InterruptedException e) {
  68.             }
  69.         }
  70.  
  71.  
  72.         if(idMap.get(Thread.currentThread()).equals(currentId) ){
  73.             currentId=null;
  74.             inform();
  75.         }
  76.  
  77.         while(currentId!=null || !idMap.get(Thread.currentThread()).equals(nextId) ){
  78.             try {
  79.                 if(koniec && nextId==null && currentId==null) {
  80.                     try {
  81.                         return false;
  82.                     }
  83.                     finally {
  84.                         inform();
  85.                     }
  86.                 }
  87.                 wait();
  88.  
  89.             } catch (InterruptedException e) {}
  90.         }
  91.  
  92.  
  93.         currentId=nextId;
  94.         nextId=null;
  95.         inform();
  96.         return true;
  97.     }
  98. }