@@ -38,6 +38,8 @@ public class BlockingCell<T> {
3838 private T _value ;
3939
4040 private static final long NANOS_IN_MILLI = 1000 * 1000 ;
41+
42+ private static final long INFINITY = -1 ;
4143
4244 /** Instantiate a new BlockingCell waiting for a value of the specified type. */
4345 public BlockingCell () {
@@ -63,14 +65,19 @@ public synchronized T get() throws InterruptedException {
6365 * already a value present, there's no need to wait - the existing value is returned.
6466 * If timeout is reached and value hasn't arrived, TimeoutException is thrown
6567 *
66- * @param timeout timeout in miliseconds.Value less than zero effectively means infinity
68+ * @param timeout timeout in miliseconds. -1 effectively means infinity
6769 * @return the waited-for value
6870 * @throws InterruptedException if this thread is interrupted
6971 */
7072 public synchronized T get (long timeout ) throws InterruptedException , TimeoutException {
71- synchronized (this ) {
72- wait (timeout );
73- }
73+ if (timeout < 0 && timeout != INFINITY )
74+ throw new AssertionError ("Timeout cannot be less than zero" );
75+
76+ if (timeout != 0 ) {
77+ synchronized (this ) {
78+ wait (timeout == INFINITY ? 0 : timeout );
79+ }
80+ }
7481
7582 if (!_filled )
7683 throw new TimeoutException ();
@@ -92,24 +99,18 @@ public synchronized T uninterruptibleGet() {
9299 }
93100 }
94101
95-
96102 /**
97103 * As get(long timeout), but catches and ignores InterruptedException, retrying until
98104 * a value appears or until specified timeout is reached. If timeout is reached,
99105 * TimeoutException it thrown.
100106 * We also use System.nanoTime() to behave correctly when system clock jumps around.
101107 *
102- * @param timeout timeout in miliseconds. 0 effectively means infinity
108+ * @param timeout timeout in miliseconds. -1 effectively means infinity
103109 * @return the waited-for value
104110 */
105111 public synchronized T uninterruptibleGet (int timeout ) throws TimeoutException {
106- long now = System .nanoTime () / NANOS_IN_MILLI ;
112+ long now = System .nanoTime () / NANOS_IN_MILLI ;
107113 long runTime = now + timeout ;
108-
109-
110- if (timeout < 0 ) {
111- throw new AssertionError ("Timeout cannot be less than zero" );
112- }
113114
114115 do {
115116 try {
@@ -119,7 +120,7 @@ public synchronized T uninterruptibleGet(int timeout) throws TimeoutException {
119120 } catch (InterruptedException e ) {
120121 // Ignore.
121122 }
122- } while ((timeout == 0 ) || ((now = System .nanoTime () / NANOS_IN_MILLI ) < runTime ));
123+ } while ((timeout == INFINITY ) || ((now = System .nanoTime () / NANOS_IN_MILLI ) < runTime ));
123124
124125 throw new TimeoutException ();
125126 }
0 commit comments