1 28 29 package org.apache.commons.transaction.util; 30 31 37 public class RendezvousBarrier { 38 39 public static final int DEFAULT_TIMEOUT = 20000; 40 41 protected final int parties; 42 protected final String name; 43 protected int count = 0; 44 protected long timeout; 45 protected LoggerFacade logger; 46 47 public RendezvousBarrier(String name, LoggerFacade logger) { 48 this(name, DEFAULT_TIMEOUT, logger); 49 } 50 51 public RendezvousBarrier(String name, long timeout, LoggerFacade logger) { 52 this(name, 2, timeout, logger); 53 } 54 55 public RendezvousBarrier(String name, int parties, long timeout, LoggerFacade logger) { 56 this.parties = parties; 57 this.name = name; 58 this.timeout = timeout; 59 this.logger = logger; 60 } 61 62 66 public synchronized void call() { 67 count++; 68 if (count >= parties) { 69 if (logger.isFineEnabled()) 70 logger.logFine("Thread " + Thread.currentThread().getName() + " by CALL COMPLETING barrier " + name); 71 notifyAll(); 72 } 73 } 74 75 82 public synchronized void meet() throws InterruptedException { 83 count++; 84 if (count >= parties) { 85 if (logger.isFineEnabled()) 86 logger.logFine("Thread " + Thread.currentThread().getName() + " by MEET COMPLETING barrier " + name); 87 notifyAll(); 88 } else { 89 if (logger.isFineEnabled()) { 90 logger.logFine( 91 "At barrier " 92 + name 93 + " thread " 94 + Thread.currentThread().getName() 95 + " WAITING for " 96 + (parties - count) 97 + " of " 98 + parties 99 + " parties"); 100 } 101 wait(timeout); 102 if (count == 0) { 103 } else if (count >= parties) { 105 if (logger.isFineEnabled()) 106 logger.logFine("Thread " + Thread.currentThread().getName() + " CONTINUING at barrier " + name); 107 } else { 108 if (logger.isFineEnabled()) 109 logger.logFine("Thread " + Thread.currentThread().getName() + " FAILING at barrier " + name); 110 notifyAll(); 111 } 112 } 113 } 114 115 118 public synchronized void reset() { 119 if (logger.isFineEnabled()) logger.logFine("Resetting barrier " + name); 120 count = 0; 121 notifyAll(); 122 } 123 124 } | Popular Tags |