@@ -38,6 +38,8 @@ public class BlockingCell<T> {
38
38
private T _value ;
39
39
40
40
private static final long NANOS_IN_MILLI = 1000 * 1000 ;
41
+
42
+ private static final long INFINITY = -1 ;
41
43
42
44
/** Instantiate a new BlockingCell waiting for a value of the specified type. */
43
45
public BlockingCell () {
@@ -63,14 +65,19 @@ public synchronized T get() throws InterruptedException {
63
65
* already a value present, there's no need to wait - the existing value is returned.
64
66
* If timeout is reached and value hasn't arrived, TimeoutException is thrown
65
67
*
66
- * @param timeout timeout in miliseconds.Value less than zero effectively means infinity
68
+ * @param timeout timeout in miliseconds. -1 effectively means infinity
67
69
* @return the waited-for value
68
70
* @throws InterruptedException if this thread is interrupted
69
71
*/
70
72
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
+ }
74
81
75
82
if (!_filled )
76
83
throw new TimeoutException ();
@@ -92,24 +99,18 @@ public synchronized T uninterruptibleGet() {
92
99
}
93
100
}
94
101
95
-
96
102
/**
97
103
* As get(long timeout), but catches and ignores InterruptedException, retrying until
98
104
* a value appears or until specified timeout is reached. If timeout is reached,
99
105
* TimeoutException it thrown.
100
106
* We also use System.nanoTime() to behave correctly when system clock jumps around.
101
107
*
102
- * @param timeout timeout in miliseconds. 0 effectively means infinity
108
+ * @param timeout timeout in miliseconds. -1 effectively means infinity
103
109
* @return the waited-for value
104
110
*/
105
111
public synchronized T uninterruptibleGet (int timeout ) throws TimeoutException {
106
- long now = System .nanoTime () / NANOS_IN_MILLI ;
112
+ long now = System .nanoTime () / NANOS_IN_MILLI ;
107
113
long runTime = now + timeout ;
108
-
109
-
110
- if (timeout < 0 ) {
111
- throw new AssertionError ("Timeout cannot be less than zero" );
112
- }
113
114
114
115
do {
115
116
try {
@@ -119,7 +120,7 @@ public synchronized T uninterruptibleGet(int timeout) throws TimeoutException {
119
120
} catch (InterruptedException e ) {
120
121
// Ignore.
121
122
}
122
- } while ((timeout == 0 ) || ((now = System .nanoTime () / NANOS_IN_MILLI ) < runTime ));
123
+ } while ((timeout == INFINITY ) || ((now = System .nanoTime () / NANOS_IN_MILLI ) < runTime ));
123
124
124
125
throw new TimeoutException ();
125
126
}
0 commit comments