KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > util > RendezvousBarrier


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java,v 1.2 2004/11/29 18:28:17 luetzkendorf Exp $
3 <<<<<<< .mine
4  * $Revision: 1.2 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.util;
30
31 /**
32  * Simple barrier that blocks until all parties have either called or have arrived at the meeting point.
33  * Very useful for testing or other purposes that require to make concurrent settings deterministic.
34  *
35  * @version $Revision$
36  */

37 public class RendezvousBarrier {
38
39     public static final int DEFAULT_TIMEOUT = 20000;
40
41     protected final int parties;
42     protected final String JavaDoc name;
43     protected int count = 0;
44     protected long timeout;
45     protected LoggerFacade logger;
46
47     public RendezvousBarrier(String JavaDoc name, LoggerFacade logger) {
48         this(name, DEFAULT_TIMEOUT, logger);
49     }
50
51     public RendezvousBarrier(String JavaDoc name, long timeout, LoggerFacade logger) {
52         this(name, 2, timeout, logger);
53     }
54
55     public RendezvousBarrier(String JavaDoc 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     /**
63      * Notify the barrier that you (the current thread) will not come to the meeting point.
64      * Same thing as {@link #meet()}, but does not not let you wait.
65      */

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     /**
76      * Meet at this barrier. The current thread will either block when there are missing parties for this barrier
77      * or it is the last one to complete this meeting and the barrier will release its block.
78      * In this case all other waiting threads will be notified.
79      *
80      * @throws InterruptedException if the current thread is interrupted while waiting
81      */

82     public synchronized void meet() throws InterruptedException JavaDoc {
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                 // means the barrier has been reset
104
} 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     /**
116      * Releases all waiting threads and resets the number of parties already arrived.
117      */

118     public synchronized void reset() {
119         if (logger.isFineEnabled()) logger.logFine("Resetting barrier " + name);
120         count = 0;
121         notifyAll();
122     }
123
124 }
Popular Tags