@@ -63,12 +63,13 @@ public synchronized T get() throws InterruptedException {
63
63
public synchronized T get (long timeout ) throws InterruptedException , TimeoutException {
64
64
if (timeout == INFINITY ) return get ();
65
65
66
- if (timeout < 0 )
66
+ if (timeout < 0 ) {
67
67
throw new AssertionError ("Timeout cannot be less than zero" );
68
+ }
68
69
69
- long maxTime = System .currentTimeMillis () + timeout ;
70
- long now ;
71
- while (!_filled && (now = System .currentTimeMillis ( )) < maxTime ) {
70
+ long now = System .nanoTime () / NANOS_IN_MILLI ;
71
+ long maxTime = now + timeout ;
72
+ while (!_filled && (now = ( System .nanoTime () / NANOS_IN_MILLI )) < maxTime ) {
72
73
wait (maxTime - now );
73
74
}
74
75
@@ -83,11 +84,19 @@ public synchronized T get(long timeout) throws InterruptedException, TimeoutExce
83
84
* @return the waited-for value
84
85
*/
85
86
public synchronized T uninterruptibleGet () {
86
- while (true ) {
87
- try {
88
- return get ();
89
- } catch (InterruptedException ex ) {
90
- // no special handling necessary
87
+ boolean wasInterrupted = false ;
88
+ try {
89
+ while (true ) {
90
+ try {
91
+ return get ();
92
+ } catch (InterruptedException ex ) {
93
+ // no special handling necessary
94
+ wasInterrupted = true ;
95
+ }
96
+ }
97
+ } finally {
98
+ if (wasInterrupted ) {
99
+ Thread .currentThread ().interrupt ();
91
100
}
92
101
}
93
102
}
@@ -104,14 +113,21 @@ public synchronized T uninterruptibleGet() {
104
113
public synchronized T uninterruptibleGet (int timeout ) throws TimeoutException {
105
114
long now = System .nanoTime () / NANOS_IN_MILLI ;
106
115
long runTime = now + timeout ;
107
-
108
- do {
109
- try {
110
- return get (runTime - now );
111
- } catch (InterruptedException e ) {
112
- // Ignore.
116
+ boolean wasInterrupted = false ;
117
+ try {
118
+ do {
119
+ try {
120
+ return get (runTime - now );
121
+ } catch (InterruptedException e ) {
122
+ // Ignore.
123
+ wasInterrupted = true ;
124
+ }
125
+ } while ((timeout == INFINITY ) || ((now = System .nanoTime () / NANOS_IN_MILLI ) < runTime ));
126
+ } finally {
127
+ if (wasInterrupted ) {
128
+ Thread .currentThread ().interrupt ();
113
129
}
114
- } while (( timeout == INFINITY ) || (( now = System . nanoTime () / NANOS_IN_MILLI ) < runTime ));
130
+ }
115
131
116
132
throw new TimeoutException ();
117
133
}
0 commit comments