01: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.utils;
07:
08: import java.util.Set;
09:
10: /**
11: * This is a weird semaphore that makes every thread wait, until
12: * all of Strings from a given set have been "checked in".
13: *
14: * @author Peter Kharchenko {@link <a href="mailto:pkharchenko@interactivebusiness.com"">pkharchenko@interactivebusiness.com"</a>}
15: * @version $Revision: 36690 $
16: */
17: public class SetCheckInSemaphore {
18: private Set registry;
19:
20: /**
21: * Creates a new <code>CountDownSemaphore</code> instance.
22: *
23: * @param registrySet a <code>Set</code> of key objects
24: * that will have to be "checked in" before any waiting threads are allowed to proceed.
25: */
26: public SetCheckInSemaphore(Set registrySet) {
27: this .registry = registrySet;
28: }
29:
30: /**
31: * Checks in with a given name, and waits for others.
32: *
33: * @param key an <code>Object</code> value
34: */
35: public synchronized void checkInAndWaitOn(Object key) {
36: registry.remove(key);
37: while (!registry.isEmpty()) {
38: try {
39: this .wait();
40: } catch (InterruptedException ie) {
41: }
42: }
43: this .notifyAll();
44: }
45:
46: /**
47: * Check in a key, but do not wait on the semaphore.
48: *
49: * @param key an <code>Object</code> value
50: */
51: public synchronized void checkIn(Object key) {
52: registry.remove(key);
53: if (registry.isEmpty()) {
54: this .notifyAll();
55: }
56: }
57:
58: /**
59: * Wait on the semaphore, without checking in any keys.
60: *
61: */
62: public synchronized void waitOn() {
63: while (!registry.isEmpty()) {
64: try {
65: this .wait();
66: } catch (InterruptedException ie) {
67: }
68: }
69: }
70:
71: /**
72: * Checks in all the remaining values, so that all
73: * threads can proceed immediately.
74: *
75: */
76: public synchronized void checkInAll() {
77: registry.clear();
78: this.notifyAll();
79: }
80:
81: }
|