1 4 package com.tctest.runner; 5 6 import com.tc.util.concurrent.ThreadUtil; 7 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 15 public class TransparentAppCoordinator { 16 private final String globalId; 17 private final int participantCount; 18 private final Map participants = new HashMap (); 19 private final boolean print = false; 20 21 public TransparentAppCoordinator(String globalId, int participantCount) { 22 this.globalId = globalId; 23 this.participantCount = participantCount; 24 } 25 26 public String getGlobalId() { 27 return this.globalId; 28 } 29 30 public int getParticipantCount() { 31 return this.participantCount; 32 } 33 34 public void moveToStageAndWait(int stage) { 35 try { 36 moveToStage(stage); 37 while (true) { 38 if (allNodesInOrBeyondStage(stage)) return; 39 ThreadUtil.reallySleep(1000); 40 println("Waiting"); 41 } 42 } finally { 43 synchronized (participants) { 44 println("Done waiting:" + participants + " stage: " + stage); 45 } 46 } 47 } 48 49 public void moveToStage(int stage) { 50 synchronized (participants) { 51 participants.put("" + globalId, new Integer (stage)); 52 } 53 } 54 55 public boolean allNodesInOrBeyondStage(int stage) { 56 synchronized (participants) { 57 println("participants: " + participants + ", participantCount: " + this.participantCount); 58 if (participants.size() != this.participantCount) return false; 59 for (Iterator i = participants.keySet().iterator(); i.hasNext();) { 60 int testStage = ((Integer ) participants.get(i.next())).intValue(); 61 if (testStage < stage) return false; 62 } 63 } 64 return true; 65 } 66 67 private void println(String line) { 68 if (print) { 69 System.out.println(Thread.currentThread() + ": " + line); 70 } 71 } 72 } | Popular Tags |