setcheckinsemaphore.java

来自「uPortal是开放源码的Portal门户产品」· Java 代码 · 共 75 行

JAVA
75
字号
package org.jasig.portal.utils;import java.util.Set;/** * This is a weird semaphore that makes every thread wait, until * all of Strings from a given set have been "checked in". * * @author <a href="mailto:pkharchenko@interactivebusiness.com">Peter Kharchenko</a> * @version $Revision: 1.1 $ */public class SetCheckInSemaphore {    private Set registry;    /**     * Creates a new <code>CountDownSemaphore</code> instance.     *     * @param registrySet a <code>Set</code> of key objects     * that will have to be "checked in" before any waiting threads are allowed to proceed.     */    public SetCheckInSemaphore(Set registrySet) {        this.registry=registrySet;    }    /**     * Checks in with a given name, and waits for others.     *     * @param key an <code>Object</code> value     */    public synchronized void checkInAndWaitOn(Object key) {        registry.remove(key);        while(!registry.isEmpty()) {            try {                this.wait();            } catch (InterruptedException ie) {}        }        this.notifyAll();    }    /**     * Check in a key, but do not wait on the semaphore.     *     * @param key an <code>Object</code> value     */    public synchronized void checkIn(Object key) {        registry.remove(key);        if(registry.isEmpty()) {            this.notifyAll();        }    }    /**     * Wait on the semaphore, without checking in any keys.     *     */    public synchronized void waitOn() {        while(!registry.isEmpty()) {            try {                this.wait();            } catch (InterruptedException ie) {}        }    }    /**     * Checks in all the remaining values, so that all     * threads can proceed immediately.     *     */    public synchronized void checkInAll() {        registry.clear();        this.notifyAll();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?