You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oz...@apache.org on 2004/06/11 14:22:56 UTC
cvs commit: jakarta-commons-sandbox/test/src/java/org/apache/commons/test/concurrent RendezvousBarrier.java
ozeigermann 2004/06/11 05:22:56
Added: test/src/java/org/apache/commons/test/concurrent
RendezvousBarrier.java
Log:
Added initial version of rendezvous barrier
Revision Changes Path
1.1 jakarta-commons-sandbox/test/src/java/org/apache/commons/test/concurrent/RendezvousBarrier.java
Index: RendezvousBarrier.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/test/src/java/org/apache/commons/test/concurrent/RendezvousBarrier.java,v 1.1 2004/06/11 12:22:56 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 12:22:56 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.test.concurrent;
/**
* Simple barrier that blocks until all parties have either called or have arrived at the meeting point.
* Very useful for testing that require to make concurrent settings deterministic.
*
* @author <a href="mailto:ozeigermann@apache.org">Oliver Zeigermann</a>
*/
public class RendezvousBarrier {
public static final int DEFAULT_TIMEOUT = 20000;
protected final int parties;
protected final String name;
protected int count = 0;
protected long timeout;
public RendezvousBarrier(String name) {
this(name, DEFAULT_TIMEOUT);
}
public RendezvousBarrier(String name, long timeout) {
this(name, 2, timeout);
}
public RendezvousBarrier(String name, int parties, long timeout) {
this.parties = parties;
this.name = name;
this.timeout = timeout;
}
/**
* Notify the barrier that you (the current thread) will not come to the meeting point.
* Same thing as {@link #meet()}, but does not not let you wait.
*/
public synchronized void call() {
count++;
if (count >= parties) {
notifyAll();
}
}
/**
* Meet at this barrier. The current thread will either block when there are missing parties for this barrier
* or it is the last one to complete this meeting and the barrier will release its block.
* In this case all other waiting threads will be notified.
*
* @throws InterruptedException if the current thread is interrupted while waiting
*/
public synchronized void meet() throws InterruptedException {
count++;
if (count >= parties) {
notifyAll();
} else {
wait(timeout);
if (count == 0) {
// means the barrier has been reset
} else if (count >= parties) {
// ok, fine, let's go one
} else {
// we have failed because of a timeout it seems
notifyAll();
}
}
}
/**
* Releases all waiting threads and resets the number of parties already arrived.
*/
public synchronized void reset() {
count = 0;
notifyAll();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org