001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.util.concurrent; 018 019import static java.util.concurrent.TimeUnit.NANOSECONDS; 020 021import com.google.common.annotations.Beta; 022import com.google.common.base.Preconditions; 023 024import java.util.concurrent.BlockingQueue; 025import java.util.concurrent.CancellationException; 026import java.util.concurrent.CountDownLatch; 027import java.util.concurrent.ExecutionException; 028import java.util.concurrent.Future; 029import java.util.concurrent.Semaphore; 030import java.util.concurrent.TimeUnit; 031import java.util.concurrent.TimeoutException; 032 033/** 034 * Utilities for treating interruptible operations as uninterruptible. 035 * In all cases, if a thread is interrupted during such a call, the call 036 * continues to block until the result is available or the timeout elapses, 037 * and only then re-interrupts the thread. 038 * 039 * @author Anthony Zana 040 * @since 10.0 041 */ 042@Beta 043public final class Uninterruptibles { 044 045 // Implementation Note: As of 3-7-11, the logic for each blocking/timeout 046 // methods is identical, save for method being invoked. 047 048 /** 049 * Invokes {@code latch.}{@link CountDownLatch#await() await()} 050 * uninterruptibly. 051 */ 052 public static void awaitUninterruptibly(CountDownLatch latch) { 053 boolean interrupted = false; 054 try { 055 while (true) { 056 try { 057 latch.await(); 058 return; 059 } catch (InterruptedException e) { 060 interrupted = true; 061 } 062 } 063 } finally { 064 if (interrupted) { 065 Thread.currentThread().interrupt(); 066 } 067 } 068 } 069 070 /** 071 * Invokes 072 * {@code latch.}{@link CountDownLatch#await(long, TimeUnit) 073 * await(timeout, unit)} uninterruptibly. 074 */ 075 public static boolean awaitUninterruptibly(CountDownLatch latch, 076 long timeout, TimeUnit unit) { 077 boolean interrupted = false; 078 try { 079 long remainingNanos = unit.toNanos(timeout); 080 long end = System.nanoTime() + remainingNanos; 081 082 while (true) { 083 try { 084 // CountDownLatch treats negative timeouts just like zero. 085 return latch.await(remainingNanos, NANOSECONDS); 086 } catch (InterruptedException e) { 087 interrupted = true; 088 remainingNanos = end - System.nanoTime(); 089 } 090 } 091 } finally { 092 if (interrupted) { 093 Thread.currentThread().interrupt(); 094 } 095 } 096 } 097 098 /** 099 * Invokes {@code toJoin.}{@link Thread#join() join()} uninterruptibly. 100 */ 101 public static void joinUninterruptibly(Thread toJoin) { 102 boolean interrupted = false; 103 try { 104 while (true) { 105 try { 106 toJoin.join(); 107 return; 108 } catch (InterruptedException e) { 109 interrupted = true; 110 } 111 } 112 } finally { 113 if (interrupted) { 114 Thread.currentThread().interrupt(); 115 } 116 } 117 } 118 119 /** 120 * Invokes {@code future.}{@link Future#get() get()} uninterruptibly. 121 * To get uninterruptibility and remove checked exceptions, see 122 * {@link Futures#getUnchecked}. 123 * 124 * <p>If instead, you wish to treat {@link InterruptedException} uniformly 125 * with other exceptions, see {@link Futures#get(Future, Class) Futures.get} 126 * or {@link Futures#makeChecked}. 127 * 128 * @throws ExecutionException if the computation threw an exception 129 * @throws CancellationException if the computation was cancelled 130 */ 131 public static <V> V getUninterruptibly(Future<V> future) 132 throws ExecutionException { 133 boolean interrupted = false; 134 try { 135 while (true) { 136 try { 137 return future.get(); 138 } catch (InterruptedException e) { 139 interrupted = true; 140 } 141 } 142 } finally { 143 if (interrupted) { 144 Thread.currentThread().interrupt(); 145 } 146 } 147 } 148 149 /** 150 * Invokes 151 * {@code future.}{@link Future#get(long, TimeUnit) get(timeout, unit)} 152 * uninterruptibly. 153 * 154 * <p>If instead, you wish to treat {@link InterruptedException} uniformly 155 * with other exceptions, see {@link Futures#get(Future, Class) Futures.get} 156 * or {@link Futures#makeChecked}. 157 * 158 * @throws ExecutionException if the computation threw an exception 159 * @throws CancellationException if the computation was cancelled 160 * @throws TimeoutException if the wait timed out 161 */ 162 public static <V> V getUninterruptibly( 163 Future<V> future, long timeout, TimeUnit unit) 164 throws ExecutionException, TimeoutException { 165 boolean interrupted = false; 166 try { 167 long remainingNanos = unit.toNanos(timeout); 168 long end = System.nanoTime() + remainingNanos; 169 170 while (true) { 171 try { 172 // Future treats negative timeouts just like zero. 173 return future.get(remainingNanos, NANOSECONDS); 174 } catch (InterruptedException e) { 175 interrupted = true; 176 remainingNanos = end - System.nanoTime(); 177 } 178 } 179 } finally { 180 if (interrupted) { 181 Thread.currentThread().interrupt(); 182 } 183 } 184 } 185 186 /** 187 * Invokes 188 * {@code unit.}{@link TimeUnit#timedJoin(Thread, long) 189 * timedJoin(toJoin, timeout)} uninterruptibly. 190 */ 191 public static void joinUninterruptibly(Thread toJoin, 192 long timeout, TimeUnit unit) { 193 Preconditions.checkNotNull(toJoin); 194 boolean interrupted = false; 195 try { 196 long remainingNanos = unit.toNanos(timeout); 197 long end = System.nanoTime() + remainingNanos; 198 while (true) { 199 try { 200 // TimeUnit.timedJoin() treats negative timeouts just like zero. 201 NANOSECONDS.timedJoin(toJoin, remainingNanos); 202 return; 203 } catch (InterruptedException e) { 204 interrupted = true; 205 remainingNanos = end - System.nanoTime(); 206 } 207 } 208 } finally { 209 if (interrupted) { 210 Thread.currentThread().interrupt(); 211 } 212 } 213 } 214 215 /** 216 * Invokes {@code queue.}{@link BlockingQueue#take() take()} uninterruptibly. 217 */ 218 public static <E> E takeUninterruptibly(BlockingQueue<E> queue) { 219 boolean interrupted = false; 220 try { 221 while (true) { 222 try { 223 return queue.take(); 224 } catch (InterruptedException e) { 225 interrupted = true; 226 } 227 } 228 } finally { 229 if (interrupted) { 230 Thread.currentThread().interrupt(); 231 } 232 } 233 } 234 235 /** 236 * Invokes {@code queue.}{@link BlockingQueue#put(Object) put(element)} 237 * uninterruptibly. 238 * 239 * @throws ClassCastException if the class of the specified element prevents 240 * it from being added to the given queue 241 * @throws IllegalArgumentException if some property of the specified element 242 * prevents it from being added to the given queue 243 */ 244 public static <E> void putUninterruptibly(BlockingQueue<E> queue, E element) { 245 boolean interrupted = false; 246 try { 247 while (true) { 248 try { 249 queue.put(element); 250 return; 251 } catch (InterruptedException e) { 252 interrupted = true; 253 } 254 } 255 } finally { 256 if (interrupted) { 257 Thread.currentThread().interrupt(); 258 } 259 } 260 } 261 262 // TODO(user): Support Sleeper somehow (wrapper or interface method)? 263 /** 264 * Invokes {@code unit.}{@link TimeUnit#sleep(long) sleep(sleepFor)} 265 * uninterruptibly. 266 */ 267 public static void sleepUninterruptibly(long sleepFor, TimeUnit unit) { 268 boolean interrupted = false; 269 try { 270 long remainingNanos = unit.toNanos(sleepFor); 271 long end = System.nanoTime() + remainingNanos; 272 while (true) { 273 try { 274 // TimeUnit.sleep() treats negative timeouts just like zero. 275 NANOSECONDS.sleep(remainingNanos); 276 return; 277 } catch (InterruptedException e) { 278 interrupted = true; 279 remainingNanos = end - System.nanoTime(); 280 } 281 } 282 } finally { 283 if (interrupted) { 284 Thread.currentThread().interrupt(); 285 } 286 } 287 } 288 289 /** 290 * Invokes {@code semaphore.}{@link Semaphore#tryAcquire(int, long, TimeUnit) 291 * tryAcquire(1, timeout, unit)} uninterruptibly. 292 * 293 * @since 18.0 294 */ 295 public static boolean tryAcquireUninterruptibly( 296 Semaphore semaphore, long timeout, TimeUnit unit) { 297 return tryAcquireUninterruptibly(semaphore, 1, timeout, unit); 298 } 299 300 /** 301 * Invokes {@code semaphore.}{@link Semaphore#tryAcquire(int, long, TimeUnit) 302 * tryAcquire(permits, timeout, unit)} uninterruptibly. 303 * 304 * @since 18.0 305 */ 306 public static boolean tryAcquireUninterruptibly( 307 Semaphore semaphore, int permits, long timeout, TimeUnit unit) { 308 boolean interrupted = false; 309 try { 310 long remainingNanos = unit.toNanos(timeout); 311 long end = System.nanoTime() + remainingNanos; 312 313 while (true) { 314 try { 315 // Semaphore treats negative timeouts just like zero. 316 return semaphore.tryAcquire(permits, remainingNanos, NANOSECONDS); 317 } catch (InterruptedException e) { 318 interrupted = true; 319 remainingNanos = end - System.nanoTime(); 320 } 321 } 322 } finally { 323 if (interrupted) { 324 Thread.currentThread().interrupt(); 325 } 326 } 327 } 328 329 // TODO(user): Add support for waitUninterruptibly. 330 331 private Uninterruptibles() {} 332}