KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/util/TurnBarrier.java,v 1.1 2005/01/07 12:41:58 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.1 $
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 turn based barrier to make a sequence of calls from different threads deterministic.
33  * This is very useful for testing where you want to have a contious flow throughout
34  * different threads. The idea is to have an ordered sequence of steps where step n can not be
35  * executed before n-1.
36  *
37  * @version $Revision$
38  */

39 public class TurnBarrier {
40
41     public static final long DEFAULT_TIMEOUT = Long.MAX_VALUE;
42
43     protected final String JavaDoc name;
44
45     protected int currentNumber;
46
47     protected final int startNumber;
48
49     protected final long timeout;
50
51     protected LoggerFacade logger;
52
53     /**
54      * Creates a new turn barrier starting with turn 0 with an unlimited timeout.
55      *
56      * @param name the name of the barrier
57      * @param logger logger for debug output
58      */

59     public TurnBarrier(String JavaDoc name, LoggerFacade logger) {
60         this(name, DEFAULT_TIMEOUT, logger);
61     }
62
63     /**
64      * Creates a new turn barrier starting with turn 0.
65      *
66      * @param name the name of the barrier
67      * @param timeout timeout for threads to wait for their turn
68      * @param logger logger for debug output
69      */

70     public TurnBarrier(String JavaDoc name, long timeout, LoggerFacade logger) {
71         this(name, timeout, logger, 0);
72     }
73
74     /**
75      * Creates a new turn barrier.
76      *
77      * @param name the name of the barrier
78      * @param timeout timeout for threads to wait for their turn
79      * @param logger logger for debug output
80      * @param startTurn the turn to start with
81      */

82     public TurnBarrier(String JavaDoc name, long timeout, LoggerFacade logger, int startTurn) {
83         this.name = name;
84         this.timeout = timeout;
85         this.logger = logger;
86         this.startNumber = startTurn;
87         this.currentNumber = startTurn;
88     }
89
90     /**
91      * Blockingly waits for the given turn. If a timeout occurs a runtime exception will be thrown.
92      *
93      * @param turnNumber the turn number to wait for
94      * @throws InterruptedException thrown if the thread is interrupted while waiting
95      * @throws RuntimeException thrown when timed out
96      */

97     public synchronized void waitForTurn(int turnNumber) throws InterruptedException JavaDoc,
98             RuntimeException JavaDoc {
99         if (turnNumber > currentNumber) {
100             long started = System.currentTimeMillis();
101             for (long remaining = timeout; remaining > 0 && turnNumber > currentNumber; remaining = timeout
102                     - (System.currentTimeMillis() - started)) {
103                 wait(remaining);
104             }
105         }
106         if (turnNumber > currentNumber) {
107             throw new RuntimeException JavaDoc("Timed out while waiting for our turn");
108         }
109     }
110
111     /**
112      * Signals the next turn. Any thread waiting for the turn will be allowed to continue.
113      *
114      * @param turnNumber the next turn number
115      */

116     public synchronized void signalTurn(int turnNumber) {
117         currentNumber = turnNumber;
118         notifyAll();
119     }
120     
121     /**
122      * Starts the barrier over again. The next turn will be the one the barrier was started with.
123      *
124      */

125     public synchronized void reset() {
126         signalTurn(startNumber);
127     }
128 }
Popular Tags