@@ -63,12 +63,13 @@ public synchronized T get() throws InterruptedException {
6363 public synchronized T get (long timeout ) throws InterruptedException , TimeoutException {
6464 if (timeout == INFINITY ) return get ();
6565
66- if (timeout < 0 )
66+ if (timeout < 0 ) {
6767 throw new AssertionError ("Timeout cannot be less than zero" );
68+ }
6869
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 ) {
7273 wait (maxTime - now );
7374 }
7475
@@ -83,11 +84,19 @@ public synchronized T get(long timeout) throws InterruptedException, TimeoutExce
8384 * @return the waited-for value
8485 */
8586 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 ();
91100 }
92101 }
93102 }
@@ -104,14 +113,21 @@ public synchronized T uninterruptibleGet() {
104113 public synchronized T uninterruptibleGet (int timeout ) throws TimeoutException {
105114 long now = System .nanoTime () / NANOS_IN_MILLI ;
106115 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 ();
113129 }
114- } while (( timeout == INFINITY ) || (( now = System . nanoTime () / NANOS_IN_MILLI ) < runTime ));
130+ }
115131
116132 throw new TimeoutException ();
117133 }
0 commit comments