001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019import static com.google.common.collect.Lists.newArrayListWithCapacity; 020import static com.google.common.util.concurrent.Callables.threadRenaming; 021import static com.google.common.util.concurrent.Internal.toNanosSaturated; 022import static com.google.common.util.concurrent.SneakyThrows.sneakyThrow; 023import static java.util.Objects.requireNonNull; 024import static java.util.concurrent.Executors.defaultThreadFactory; 025import static java.util.concurrent.Executors.unconfigurableExecutorService; 026import static java.util.concurrent.Executors.unconfigurableScheduledExecutorService; 027import static java.util.concurrent.TimeUnit.NANOSECONDS; 028import static java.util.concurrent.TimeUnit.SECONDS; 029 030import com.google.common.annotations.GwtCompatible; 031import com.google.common.annotations.GwtIncompatible; 032import com.google.common.annotations.J2ktIncompatible; 033import com.google.common.annotations.VisibleForTesting; 034import com.google.common.base.Supplier; 035import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture; 036import com.google.errorprone.annotations.CanIgnoreReturnValue; 037import java.lang.reflect.InvocationTargetException; 038import java.time.Duration; 039import java.util.Collection; 040import java.util.Iterator; 041import java.util.List; 042import java.util.concurrent.BlockingQueue; 043import java.util.concurrent.Callable; 044import java.util.concurrent.Delayed; 045import java.util.concurrent.ExecutionException; 046import java.util.concurrent.Executor; 047import java.util.concurrent.ExecutorService; 048import java.util.concurrent.Executors; 049import java.util.concurrent.Future; 050import java.util.concurrent.LinkedBlockingQueue; 051import java.util.concurrent.RejectedExecutionException; 052import java.util.concurrent.ScheduledExecutorService; 053import java.util.concurrent.ScheduledFuture; 054import java.util.concurrent.ScheduledThreadPoolExecutor; 055import java.util.concurrent.ThreadFactory; 056import java.util.concurrent.ThreadPoolExecutor; 057import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; 058import java.util.concurrent.TimeUnit; 059import java.util.concurrent.TimeoutException; 060import org.jspecify.annotations.Nullable; 061 062/** 063 * Factory and utility methods for {@link Executor}, {@link ExecutorService}, and {@link 064 * ThreadFactory}. 065 * 066 * @author Eric Fellheimer 067 * @author Kyle Littlefield 068 * @author Justin Mahoney 069 * @since 3.0 070 */ 071@GwtCompatible 072public final class MoreExecutors { 073 private MoreExecutors() {} 074 075 /** 076 * Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application 077 * is complete. It does so by using daemon threads and adding a shutdown hook to wait for their 078 * completion. 079 * 080 * <p>This is mainly for fixed thread pools. See {@link Executors#newFixedThreadPool(int)}. 081 * 082 * @param executor the executor to modify to make sure it exits when the application is finished 083 * @param terminationTimeout how long to wait for the executor to finish before terminating the 084 * JVM 085 * @return an unmodifiable version of the input which will not hang the JVM 086 * @since 28.0 (but only since 33.4.0 in the Android flavor) 087 */ 088 @J2ktIncompatible 089 @GwtIncompatible // TODO 090 public static ExecutorService getExitingExecutorService( 091 ThreadPoolExecutor executor, Duration terminationTimeout) { 092 return getExitingExecutorService(executor, toNanosSaturated(terminationTimeout), NANOSECONDS); 093 } 094 095 /** 096 * Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application 097 * is complete. It does so by using daemon threads and adding a shutdown hook to wait for their 098 * completion. 099 * 100 * <p>This is mainly for fixed thread pools. See {@link Executors#newFixedThreadPool(int)}. 101 * 102 * @param executor the executor to modify to make sure it exits when the application is finished 103 * @param terminationTimeout how long to wait for the executor to finish before terminating the 104 * JVM 105 * @param timeUnit unit of time for the time parameter 106 * @return an unmodifiable version of the input which will not hang the JVM 107 */ 108 @J2ktIncompatible 109 @GwtIncompatible // TODO 110 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 111 public static ExecutorService getExitingExecutorService( 112 ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) { 113 return new Application().getExitingExecutorService(executor, terminationTimeout, timeUnit); 114 } 115 116 /** 117 * Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application 118 * is complete. It does so by using daemon threads and adding a shutdown hook to wait for their 119 * completion. 120 * 121 * <p>This method waits 120 seconds before continuing with JVM termination, even if the executor 122 * has not finished its work. 123 * 124 * <p>This is mainly for fixed thread pools. See {@link Executors#newFixedThreadPool(int)}. 125 * 126 * @param executor the executor to modify to make sure it exits when the application is finished 127 * @return an unmodifiable version of the input which will not hang the JVM 128 */ 129 @J2ktIncompatible 130 @GwtIncompatible // concurrency 131 public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor) { 132 return new Application().getExitingExecutorService(executor); 133 } 134 135 /** 136 * Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when 137 * the application is complete. It does so by using daemon threads and adding a shutdown hook to 138 * wait for their completion. 139 * 140 * <p>This is mainly for fixed thread pools. See {@link Executors#newScheduledThreadPool(int)}. 141 * 142 * @param executor the executor to modify to make sure it exits when the application is finished 143 * @param terminationTimeout how long to wait for the executor to finish before terminating the 144 * JVM 145 * @return an unmodifiable version of the input which will not hang the JVM 146 * @since 28.0 (but only since 33.4.0 in the Android flavor) 147 */ 148 @J2ktIncompatible 149 @GwtIncompatible // java.time.Duration 150 public static ScheduledExecutorService getExitingScheduledExecutorService( 151 ScheduledThreadPoolExecutor executor, Duration terminationTimeout) { 152 return getExitingScheduledExecutorService( 153 executor, toNanosSaturated(terminationTimeout), NANOSECONDS); 154 } 155 156 /** 157 * Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when 158 * the application is complete. It does so by using daemon threads and adding a shutdown hook to 159 * wait for their completion. 160 * 161 * <p>This is mainly for fixed thread pools. See {@link Executors#newScheduledThreadPool(int)}. 162 * 163 * @param executor the executor to modify to make sure it exits when the application is finished 164 * @param terminationTimeout how long to wait for the executor to finish before terminating the 165 * JVM 166 * @param timeUnit unit of time for the time parameter 167 * @return an unmodifiable version of the input which will not hang the JVM 168 */ 169 @J2ktIncompatible 170 @GwtIncompatible // TODO 171 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 172 public static ScheduledExecutorService getExitingScheduledExecutorService( 173 ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) { 174 return new Application() 175 .getExitingScheduledExecutorService(executor, terminationTimeout, timeUnit); 176 } 177 178 /** 179 * Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when 180 * the application is complete. It does so by using daemon threads and adding a shutdown hook to 181 * wait for their completion. 182 * 183 * <p>This method waits 120 seconds before continuing with JVM termination, even if the executor 184 * has not finished its work. 185 * 186 * <p>This is mainly for fixed thread pools. See {@link Executors#newScheduledThreadPool(int)}. 187 * 188 * @param executor the executor to modify to make sure it exits when the application is finished 189 * @return an unmodifiable version of the input which will not hang the JVM 190 */ 191 @J2ktIncompatible 192 @GwtIncompatible // TODO 193 public static ScheduledExecutorService getExitingScheduledExecutorService( 194 ScheduledThreadPoolExecutor executor) { 195 return new Application().getExitingScheduledExecutorService(executor); 196 } 197 198 /** 199 * Add a shutdown hook to wait for thread completion in the given {@link ExecutorService service}. 200 * This is useful if the given service uses daemon threads, and we want to keep the JVM from 201 * exiting immediately on shutdown, instead giving these daemon threads a chance to terminate 202 * normally. 203 * 204 * @param service ExecutorService which uses daemon threads 205 * @param terminationTimeout how long to wait for the executor to finish before terminating the 206 * JVM 207 * @since 28.0 (but only since 33.4.0 in the Android flavor) 208 */ 209 @J2ktIncompatible 210 @GwtIncompatible // java.time.Duration 211 public static void addDelayedShutdownHook(ExecutorService service, Duration terminationTimeout) { 212 addDelayedShutdownHook(service, toNanosSaturated(terminationTimeout), NANOSECONDS); 213 } 214 215 /** 216 * Add a shutdown hook to wait for thread completion in the given {@link ExecutorService service}. 217 * This is useful if the given service uses daemon threads, and we want to keep the JVM from 218 * exiting immediately on shutdown, instead giving these daemon threads a chance to terminate 219 * normally. 220 * 221 * @param service ExecutorService which uses daemon threads 222 * @param terminationTimeout how long to wait for the executor to finish before terminating the 223 * JVM 224 * @param timeUnit unit of time for the time parameter 225 */ 226 @J2ktIncompatible 227 @GwtIncompatible // TODO 228 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 229 public static void addDelayedShutdownHook( 230 ExecutorService service, long terminationTimeout, TimeUnit timeUnit) { 231 new Application().addDelayedShutdownHook(service, terminationTimeout, timeUnit); 232 } 233 234 /** Represents the current application to register shutdown hooks. */ 235 @J2ktIncompatible 236 @GwtIncompatible // TODO 237 @VisibleForTesting 238 static class Application { 239 240 final ExecutorService getExitingExecutorService( 241 ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) { 242 useDaemonThreadFactory(executor); 243 ExecutorService service = unconfigurableExecutorService(executor); 244 addDelayedShutdownHook(executor, terminationTimeout, timeUnit); 245 return service; 246 } 247 248 final ExecutorService getExitingExecutorService(ThreadPoolExecutor executor) { 249 return getExitingExecutorService(executor, 120, SECONDS); 250 } 251 252 final ScheduledExecutorService getExitingScheduledExecutorService( 253 ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) { 254 useDaemonThreadFactory(executor); 255 ScheduledExecutorService service = unconfigurableScheduledExecutorService(executor); 256 addDelayedShutdownHook(executor, terminationTimeout, timeUnit); 257 return service; 258 } 259 260 final ScheduledExecutorService getExitingScheduledExecutorService( 261 ScheduledThreadPoolExecutor executor) { 262 return getExitingScheduledExecutorService(executor, 120, SECONDS); 263 } 264 265 final void addDelayedShutdownHook( 266 ExecutorService service, long terminationTimeout, TimeUnit timeUnit) { 267 checkNotNull(service); 268 checkNotNull(timeUnit); 269 addShutdownHook( 270 newThread( 271 "DelayedShutdownHook-for-" + service, 272 () -> { 273 service.shutdown(); 274 try { 275 // We'd like to log progress and failures that may arise in the 276 // following code, but unfortunately the behavior of logging 277 // is undefined in shutdown hooks. 278 // This is because the logging code installs a shutdown hook of its 279 // own. See Cleaner class inside {@link LogManager}. 280 service.awaitTermination(terminationTimeout, timeUnit); 281 } catch (InterruptedException ignored) { 282 // We're shutting down anyway, so just ignore. 283 } 284 })); 285 } 286 287 @VisibleForTesting 288 void addShutdownHook(Thread hook) { 289 Runtime.getRuntime().addShutdownHook(hook); 290 } 291 292 Application() {} 293 } 294 295 @J2ktIncompatible 296 @GwtIncompatible // TODO 297 private static void useDaemonThreadFactory(ThreadPoolExecutor executor) { 298 executor.setThreadFactory( 299 new ThreadFactoryBuilder() 300 .setDaemon(true) 301 .setThreadFactory(executor.getThreadFactory()) 302 .build()); 303 } 304 305 /** 306 * Creates an executor service that runs each task in the thread that invokes {@code 307 * execute/submit}, as in {@link CallerRunsPolicy} This applies both to individually submitted 308 * tasks and to collections of tasks submitted via {@code invokeAll} or {@code invokeAny}. In the 309 * latter case, tasks will run serially on the calling thread. Tasks are run to completion before 310 * a {@code Future} is returned to the caller (unless the executor has been shutdown). 311 * 312 * <p>Although all tasks are immediately executed in the thread that submitted the task, this 313 * {@code ExecutorService} imposes a small locking overhead on each task submission in order to 314 * implement shutdown and termination behavior. 315 * 316 * <p>The implementation deviates from the {@code ExecutorService} specification with regards to 317 * the {@code shutdownNow} method. First, "best-effort" with regards to canceling running tasks is 318 * implemented as "no-effort". No interrupts or other attempts are made to stop threads executing 319 * tasks. Second, the returned list will always be empty, as any submitted task is considered to 320 * have started execution. This applies also to tasks given to {@code invokeAll} or {@code 321 * invokeAny} which are pending serial execution, even the subset of the tasks that have not yet 322 * started execution. It is unclear from the {@code ExecutorService} specification if these should 323 * be included, and it's much easier to implement the interpretation that they not be. Finally, a 324 * call to {@code shutdown} or {@code shutdownNow} may result in concurrent calls to {@code 325 * invokeAll/invokeAny} throwing RejectedExecutionException, although a subset of the tasks may 326 * already have been executed. 327 * 328 * @since 18.0 (present as MoreExecutors.sameThreadExecutor() since 10.0) 329 */ 330 @GwtIncompatible // TODO 331 public static ListeningExecutorService newDirectExecutorService() { 332 return new DirectExecutorService(); 333 } 334 335 /** 336 * Returns an {@link Executor} that runs each task in the thread that invokes {@link 337 * Executor#execute execute}, as in {@link CallerRunsPolicy}. 338 * 339 * <p>This executor is appropriate for tasks that are lightweight and not deeply chained. 340 * Inappropriate {@code directExecutor} usage can cause problems, and these problems can be 341 * difficult to reproduce because they depend on timing. For example: 342 * 343 * <ul> 344 * <li>When a {@code ListenableFuture} listener is registered to run under {@code 345 * directExecutor}, the listener can execute in any of three possible threads: 346 * <ol> 347 * <li>When a thread attaches a listener to a {@code ListenableFuture} that's already 348 * complete, the listener runs immediately in that thread. 349 * <li>When a thread attaches a listener to a {@code ListenableFuture} that's 350 * <em>in</em>complete and the {@code ListenableFuture} later completes normally, the 351 * listener runs in the thread that completes the {@code ListenableFuture}. 352 * <li>When a listener is attached to a {@code ListenableFuture} and the {@code 353 * ListenableFuture} gets cancelled, the listener runs immediately in the thread that 354 * cancelled the {@code Future}. 355 * </ol> 356 * Given all these possibilities, it is frequently possible for listeners to execute in UI 357 * threads, RPC network threads, or other latency-sensitive threads. In those cases, slow 358 * listeners can harm responsiveness, slow the system as a whole, or worse. (See also the 359 * note about locking below.) 360 * <li>If many tasks will be triggered by the same event, one heavyweight task may delay other 361 * tasks -- even tasks that are not themselves {@code directExecutor} tasks. 362 * <li>If many such tasks are chained together (such as with {@code 363 * future.transform(...).transform(...).transform(...)....}), they may overflow the stack. 364 * (In simple cases, callers can avoid this by registering all tasks with the same {@link 365 * MoreExecutors#newSequentialExecutor} wrapper around {@code directExecutor()}. More 366 * complex cases may require using thread pools or making deeper changes.) 367 * <li>If an exception propagates out of a {@code Runnable}, it is not necessarily seen by any 368 * {@code UncaughtExceptionHandler} for the thread. For example, if the callback passed to 369 * {@link Futures#addCallback} throws an exception, that exception will be typically be 370 * logged by the {@link ListenableFuture} implementation, even if the thread is configured 371 * to do something different. In other cases, no code will catch the exception, and it may 372 * terminate whichever thread happens to trigger the execution. 373 * </ul> 374 * 375 * A specific warning about locking: Code that executes user-supplied tasks, such as {@code 376 * ListenableFuture} listeners, should take care not to do so while holding a lock. Additionally, 377 * as a further line of defense, prefer not to perform any locking inside a task that will be run 378 * under {@code directExecutor}: Not only might the wait for a lock be long, but if the running 379 * thread was holding a lock, the listener may deadlock or break lock isolation. 380 * 381 * <p>This instance is equivalent to: 382 * 383 * {@snippet : 384 * final class DirectExecutor implements Executor { 385 * public void execute(Runnable r) { 386 * r.run(); 387 * } 388 * } 389 * } 390 * 391 * <p>This should be preferred to {@link #newDirectExecutorService()} because implementing the 392 * {@link ExecutorService} subinterface necessitates significant performance overhead. 393 * 394 * @since 18.0 395 */ 396 public static Executor directExecutor() { 397 return DirectExecutor.INSTANCE; 398 } 399 400 /** 401 * Returns an {@link Executor} that runs each task executed sequentially, such that no two tasks 402 * are running concurrently. 403 * 404 * <p>{@linkplain Executor#execute executed} tasks have a happens-before order as defined in the 405 * Java Language Specification. Tasks execute with the same happens-before order that the function 406 * calls to {@link Executor#execute execute()} that submitted those tasks had. 407 * 408 * <p>The executor uses {@code delegate} in order to {@link Executor#execute execute} each task in 409 * turn, and does not create any threads of its own. 410 * 411 * <p>After execution begins on a thread from the {@code delegate} {@link Executor}, tasks are 412 * polled and executed from a task queue until there are no more tasks. The thread will not be 413 * released until there are no more tasks to run. 414 * 415 * <p>If a task is submitted while a thread is executing tasks from the task queue, the thread 416 * will not be released until that submitted task is also complete. 417 * 418 * <p>If a task is {@linkplain Thread#interrupt interrupted} while a task is running: 419 * 420 * <ol> 421 * <li>execution will not stop until the task queue is empty. 422 * <li>tasks will begin execution with the thread marked as not interrupted - any interruption 423 * applies only to the task that was running at the point of interruption. 424 * <li>if the thread was interrupted before the SequentialExecutor's worker begins execution, 425 * the interrupt will be restored to the thread after it completes so that its {@code 426 * delegate} Executor may process the interrupt. 427 * <li>subtasks are run with the thread uninterrupted and interrupts received during execution 428 * of a task are ignored. 429 * </ol> 430 * 431 * <p>{@code RuntimeException}s thrown by tasks are simply logged and the executor keeps trucking. 432 * If an {@code Error} is thrown, the error will propagate and execution will stop until the next 433 * time a task is submitted. 434 * 435 * <p>When an {@code Error} is thrown by an executed task, previously submitted tasks may never 436 * run. An attempt will be made to restart execution on the next call to {@code execute}. If the 437 * {@code delegate} has begun to reject execution, the previously submitted tasks may never run, 438 * despite not throwing a RejectedExecutionException synchronously with the call to {@code 439 * execute}. If this behaviour is problematic, use an Executor with a single thread (e.g. {@link 440 * Executors#newSingleThreadExecutor}). 441 * 442 * @since 23.3 (since 23.1 as {@code sequentialExecutor}) 443 */ 444 @GwtIncompatible 445 public static Executor newSequentialExecutor(Executor delegate) { 446 return new SequentialExecutor(delegate); 447 } 448 449 /** 450 * Creates an {@link ExecutorService} whose {@code submit} and {@code invokeAll} methods submit 451 * {@link ListenableFutureTask} instances to the given delegate executor. Those methods, as well 452 * as {@code execute} and {@code invokeAny}, are implemented in terms of calls to {@code 453 * delegate.execute}. All other methods are forwarded unchanged to the delegate. This implies that 454 * the returned {@code ListeningExecutorService} never calls the delegate's {@code submit}, {@code 455 * invokeAll}, and {@code invokeAny} methods, so any special handling of tasks must be implemented 456 * in the delegate's {@code execute} method or by wrapping the returned {@code 457 * ListeningExecutorService}. 458 * 459 * <p>If the delegate executor was already an instance of {@code ListeningExecutorService}, it is 460 * returned untouched, and the rest of this documentation does not apply. 461 * 462 * @since 10.0 463 */ 464 @GwtIncompatible // TODO 465 public static ListeningExecutorService listeningDecorator(ExecutorService delegate) { 466 return (delegate instanceof ListeningExecutorService) 467 ? (ListeningExecutorService) delegate 468 : (delegate instanceof ScheduledExecutorService) 469 ? new ScheduledListeningDecorator((ScheduledExecutorService) delegate) 470 : new ListeningDecorator(delegate); 471 } 472 473 /** 474 * Creates a {@link ScheduledExecutorService} whose {@code submit} and {@code invokeAll} methods 475 * submit {@link ListenableFutureTask} instances to the given delegate executor. Those methods, as 476 * well as {@code execute} and {@code invokeAny}, are implemented in terms of calls to {@code 477 * delegate.execute}. All other methods are forwarded unchanged to the delegate. This implies that 478 * the returned {@code ListeningScheduledExecutorService} never calls the delegate's {@code 479 * submit}, {@code invokeAll}, and {@code invokeAny} methods, so any special handling of tasks 480 * must be implemented in the delegate's {@code execute} method or by wrapping the returned {@code 481 * ListeningScheduledExecutorService}. 482 * 483 * <p>If the delegate executor was already an instance of {@code 484 * ListeningScheduledExecutorService}, it is returned untouched, and the rest of this 485 * documentation does not apply. 486 * 487 * @since 10.0 488 */ 489 @GwtIncompatible // TODO 490 public static ListeningScheduledExecutorService listeningDecorator( 491 ScheduledExecutorService delegate) { 492 return (delegate instanceof ListeningScheduledExecutorService) 493 ? (ListeningScheduledExecutorService) delegate 494 : new ScheduledListeningDecorator(delegate); 495 } 496 497 @GwtIncompatible // TODO 498 private static class ListeningDecorator extends AbstractListeningExecutorService { 499 private final ExecutorService delegate; 500 501 ListeningDecorator(ExecutorService delegate) { 502 this.delegate = checkNotNull(delegate); 503 } 504 505 @Override 506 public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { 507 return delegate.awaitTermination(timeout, unit); 508 } 509 510 @Override 511 public final boolean isShutdown() { 512 return delegate.isShutdown(); 513 } 514 515 @Override 516 public final boolean isTerminated() { 517 return delegate.isTerminated(); 518 } 519 520 @Override 521 public final void shutdown() { 522 delegate.shutdown(); 523 } 524 525 @Override 526 public final List<Runnable> shutdownNow() { 527 return delegate.shutdownNow(); 528 } 529 530 /* 531 * TODO(user): In addition to overriding `execute`, also 532 * override the `Future`-returning methods of `ExecutorService` to propagate cancellation from 533 * our `TrustedListenableFutureTask` to a `Future` returned by the delegate executor? 534 */ 535 536 @Override 537 public final void execute(Runnable command) { 538 delegate.execute(command); 539 } 540 541 @Override 542 public final String toString() { 543 return super.toString() + "[" + delegate + "]"; 544 } 545 } 546 547 @GwtIncompatible // TODO 548 private static final class ScheduledListeningDecorator extends ListeningDecorator 549 implements ListeningScheduledExecutorService { 550 @SuppressWarnings("hiding") 551 final ScheduledExecutorService delegate; 552 553 ScheduledListeningDecorator(ScheduledExecutorService delegate) { 554 super(delegate); 555 this.delegate = checkNotNull(delegate); 556 } 557 558 @Override 559 public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { 560 TrustedListenableFutureTask<@Nullable Void> task = 561 TrustedListenableFutureTask.create(command, null); 562 ScheduledFuture<?> scheduled = delegate.schedule(task, delay, unit); 563 return new ListenableScheduledTask<@Nullable Void>(task, scheduled); 564 } 565 566 @Override 567 public <V extends @Nullable Object> ListenableScheduledFuture<V> schedule( 568 Callable<V> callable, long delay, TimeUnit unit) { 569 TrustedListenableFutureTask<V> task = TrustedListenableFutureTask.create(callable); 570 ScheduledFuture<?> scheduled = delegate.schedule(task, delay, unit); 571 return new ListenableScheduledTask<>(task, scheduled); 572 } 573 574 @Override 575 public ListenableScheduledFuture<?> scheduleAtFixedRate( 576 Runnable command, long initialDelay, long period, TimeUnit unit) { 577 NeverSuccessfulListenableFutureTask task = new NeverSuccessfulListenableFutureTask(command); 578 ScheduledFuture<?> scheduled = delegate.scheduleAtFixedRate(task, initialDelay, period, unit); 579 return new ListenableScheduledTask<@Nullable Void>(task, scheduled); 580 } 581 582 @Override 583 public ListenableScheduledFuture<?> scheduleWithFixedDelay( 584 Runnable command, long initialDelay, long delay, TimeUnit unit) { 585 NeverSuccessfulListenableFutureTask task = new NeverSuccessfulListenableFutureTask(command); 586 ScheduledFuture<?> scheduled = 587 delegate.scheduleWithFixedDelay(task, initialDelay, delay, unit); 588 return new ListenableScheduledTask<@Nullable Void>(task, scheduled); 589 } 590 591 private static final class ListenableScheduledTask<V extends @Nullable Object> 592 extends SimpleForwardingListenableFuture<V> implements ListenableScheduledFuture<V> { 593 594 private final ScheduledFuture<?> scheduledDelegate; 595 596 ListenableScheduledTask( 597 ListenableFuture<V> listenableDelegate, ScheduledFuture<?> scheduledDelegate) { 598 super(listenableDelegate); 599 this.scheduledDelegate = scheduledDelegate; 600 } 601 602 @Override 603 public boolean cancel(boolean mayInterruptIfRunning) { 604 boolean cancelled = super.cancel(mayInterruptIfRunning); 605 if (cancelled) { 606 // Unless it is cancelled, the delegate may continue being scheduled 607 scheduledDelegate.cancel(mayInterruptIfRunning); 608 609 /* 610 * We'd love to also arrange for the inverse -- that is, to also automatically cancel this 611 * future if scheduledDelegate is cancelled (as happens after the delegate executor is 612 * shut down: https://github.com/google/guava/issues/3553). But it seems unlikely that 613 * that's possible to detect in general. 614 */ 615 } 616 return cancelled; 617 } 618 619 @Override 620 public long getDelay(TimeUnit unit) { 621 return scheduledDelegate.getDelay(unit); 622 } 623 624 @Override 625 public int compareTo(Delayed other) { 626 return scheduledDelegate.compareTo(other); 627 } 628 } 629 630 private static final class NeverSuccessfulListenableFutureTask 631 extends AbstractFuture.TrustedFuture<@Nullable Void> implements Runnable { 632 private final Runnable delegate; 633 634 NeverSuccessfulListenableFutureTask(Runnable delegate) { 635 this.delegate = checkNotNull(delegate); 636 } 637 638 @Override 639 public void run() { 640 try { 641 delegate.run(); 642 } catch (Throwable t) { 643 // Any Exception is either a RuntimeException or sneaky checked exception. 644 645 /* 646 * We fail this `ListenableFuture`, whose result is exposed to the user through the 647 * `ListenableScheduledTask` we return from the `schedule*` methods. 648 */ 649 setException(t); 650 651 /* 652 * We fail the current run of the recurring task so that it is not rescheduled. This also 653 * fails the `ScheduledFuture`, which might be visible only to users who operate directly 654 * on the delegate executor's queue. 655 * 656 * (Users who try to operate directly on the `ScheduledFuture` may have additional 657 * problems. For example, if they cancel that `Future`, it won't cancel the user-visible 658 * `ListenableScheduledTask`. This is essentially the same problem as the 659 * `ListenableScheduledTask` has with executor shutdown: 660 * https://github.com/google/guava/issues/3553) 661 */ 662 throw t; 663 } 664 } 665 666 @Override 667 protected String pendingToString() { 668 return "task=[" + delegate + "]"; 669 } 670 } 671 } 672 673 /* 674 * This following method is a modified version of one found in 675 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/test/tck/AbstractExecutorServiceTest.java?revision=1.30 676 * which contained the following notice: 677 * 678 * Written by Doug Lea with assistance from members of JCP JSR-166 Expert Group and released to 679 * the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/ 680 * 681 * Other contributors include Andrew Wright, Jeffrey Hayes, Pat Fisher, Mike Judd. 682 */ 683 684 /** 685 * An implementation of {@link ExecutorService#invokeAny} for {@link ListeningExecutorService} 686 * implementations. 687 */ 688 @J2ktIncompatible 689 @GwtIncompatible 690 @ParametricNullness 691 static <T extends @Nullable Object> T invokeAnyImpl( 692 ListeningExecutorService executorService, 693 Collection<? extends Callable<T>> tasks, 694 boolean timed, 695 Duration timeout) 696 throws InterruptedException, ExecutionException, TimeoutException { 697 return invokeAnyImpl(executorService, tasks, timed, toNanosSaturated(timeout), NANOSECONDS); 698 } 699 700 /** 701 * An implementation of {@link ExecutorService#invokeAny} for {@link ListeningExecutorService} 702 * implementations. 703 */ 704 @SuppressWarnings({ 705 "GoodTime", // should accept a java.time.Duration 706 "CatchingUnchecked", // sneaky checked exception 707 "Interruption", // We copy AbstractExecutorService.invokeAny. Maybe we shouldn't: b/227335009. 708 }) 709 @J2ktIncompatible 710 @GwtIncompatible 711 @ParametricNullness 712 static <T extends @Nullable Object> T invokeAnyImpl( 713 ListeningExecutorService executorService, 714 Collection<? extends Callable<T>> tasks, 715 boolean timed, 716 long timeout, 717 TimeUnit unit) 718 throws InterruptedException, ExecutionException, TimeoutException { 719 checkNotNull(executorService); 720 checkNotNull(unit); 721 int ntasks = tasks.size(); 722 checkArgument(ntasks > 0); 723 List<Future<T>> futures = newArrayListWithCapacity(ntasks); 724 BlockingQueue<Future<T>> futureQueue = new LinkedBlockingQueue<>(); 725 long timeoutNanos = unit.toNanos(timeout); 726 727 // For efficiency, especially in executors with limited 728 // parallelism, check to see if previously submitted tasks are 729 // done before submitting more of them. This interleaving 730 // plus the exception mechanics account for messiness of main 731 // loop. 732 733 try { 734 // Record exceptions so that if we fail to obtain any 735 // result, we can throw the last exception we got. 736 ExecutionException ee = null; 737 long lastTime = timed ? System.nanoTime() : 0; 738 Iterator<? extends Callable<T>> it = tasks.iterator(); 739 740 futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue)); 741 --ntasks; 742 int active = 1; 743 744 while (true) { 745 Future<T> f = futureQueue.poll(); 746 if (f == null) { 747 if (ntasks > 0) { 748 --ntasks; 749 futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue)); 750 ++active; 751 } else if (active == 0) { 752 break; 753 } else if (timed) { 754 f = futureQueue.poll(timeoutNanos, NANOSECONDS); 755 if (f == null) { 756 throw new TimeoutException(); 757 } 758 long now = System.nanoTime(); 759 timeoutNanos -= now - lastTime; 760 lastTime = now; 761 } else { 762 f = futureQueue.take(); 763 } 764 } 765 if (f != null) { 766 --active; 767 try { 768 return f.get(); 769 } catch (ExecutionException eex) { 770 ee = eex; 771 } catch (InterruptedException iex) { 772 throw iex; 773 } catch (Exception rex) { // sneaky checked exception 774 ee = new ExecutionException(rex); 775 } 776 } 777 } 778 779 if (ee == null) { 780 ee = new ExecutionException(null); 781 } 782 throw ee; 783 } finally { 784 for (Future<T> f : futures) { 785 f.cancel(true); 786 } 787 } 788 } 789 790 /** 791 * Submits the task and adds a listener that adds the future to {@code queue} when it completes. 792 */ 793 @J2ktIncompatible 794 @GwtIncompatible // TODO 795 private static <T extends @Nullable Object> ListenableFuture<T> submitAndAddQueueListener( 796 ListeningExecutorService executorService, Callable<T> task, BlockingQueue<Future<T>> queue) { 797 ListenableFuture<T> future = executorService.submit(task); 798 future.addListener(() -> queue.add(future), directExecutor()); 799 return future; 800 } 801 802 /** 803 * Returns a default thread factory used to create new threads. 804 * 805 * <p>When running on AppEngine with access to <a 806 * href="https://cloud.google.com/appengine/docs/standard/java/javadoc/">AppEngine legacy 807 * APIs</a>, this method returns {@code ThreadManager.currentRequestThreadFactory()}. Otherwise, 808 * it returns {@link Executors#defaultThreadFactory()}. 809 * 810 * @since 14.0 811 */ 812 @J2ktIncompatible 813 @GwtIncompatible // concurrency 814 public static ThreadFactory platformThreadFactory() { 815 if (!isAppEngineWithApiClasses()) { 816 return defaultThreadFactory(); 817 } 818 try { 819 return (ThreadFactory) 820 Class.forName("com.google.appengine.api.ThreadManager") 821 .getMethod("currentRequestThreadFactory") 822 .invoke(null); 823 } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) { 824 throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e); 825 } catch (InvocationTargetException e) { 826 // `currentRequestThreadFactory` has no `throws` clause. 827 throw sneakyThrow(e.getCause()); 828 } 829 } 830 831 @J2ktIncompatible 832 @GwtIncompatible // TODO 833 private static boolean isAppEngineWithApiClasses() { 834 if (System.getProperty("com.google.appengine.runtime.environment") == null) { 835 return false; 836 } 837 try { 838 Class.forName("com.google.appengine.api.utils.SystemProperty"); 839 } catch (ClassNotFoundException e) { 840 return false; 841 } 842 try { 843 // If the current environment is null, we're not inside AppEngine. 844 return Class.forName("com.google.apphosting.api.ApiProxy") 845 .getMethod("getCurrentEnvironment") 846 .invoke(null) 847 != null; 848 } catch (ClassNotFoundException e) { 849 // If ApiProxy doesn't exist, we're not on AppEngine at all. 850 return false; 851 } catch (InvocationTargetException e) { 852 // If ApiProxy throws an exception, we're not in a proper AppEngine environment. 853 return false; 854 } catch (IllegalAccessException e) { 855 // If the method isn't accessible, we're not on a supported version of AppEngine; 856 return false; 857 } catch (NoSuchMethodException e) { 858 // If the method doesn't exist, we're not on a supported version of AppEngine; 859 return false; 860 } 861 } 862 863 /** 864 * Creates a thread using {@link #platformThreadFactory}, and sets its name to {@code name} unless 865 * changing the name is forbidden by the security manager. 866 */ 867 @J2ktIncompatible 868 @GwtIncompatible // concurrency 869 static Thread newThread(String name, Runnable runnable) { 870 checkNotNull(name); 871 checkNotNull(runnable); 872 // TODO(b/139726489): Confirm that null is impossible here. 873 Thread result = requireNonNull(platformThreadFactory().newThread(runnable)); 874 try { 875 result.setName(name); 876 } catch (SecurityException e) { 877 // OK if we can't set the name in this environment. 878 } 879 return result; 880 } 881 882 // TODO(lukes): provide overloads for ListeningExecutorService? ListeningScheduledExecutorService? 883 // TODO(lukes): provide overloads that take constant strings? Function<Runnable, String>s to 884 // calculate names? 885 886 /** 887 * Creates an {@link Executor} that renames the {@link Thread threads} that its tasks run in. 888 * 889 * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed 890 * right before each task is run. The renaming is best effort, if a {@link SecurityManager} 891 * prevents the renaming then it will be skipped but the tasks will still execute. 892 * 893 * @param executor The executor to decorate 894 * @param nameSupplier The source of names for each task 895 */ 896 @J2ktIncompatible 897 @GwtIncompatible // concurrency 898 static Executor renamingDecorator(Executor executor, Supplier<String> nameSupplier) { 899 checkNotNull(executor); 900 checkNotNull(nameSupplier); 901 return command -> executor.execute(threadRenaming(command, nameSupplier)); 902 } 903 904 /** 905 * Creates an {@link ExecutorService} that renames the {@link Thread threads} that its tasks run 906 * in. 907 * 908 * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed 909 * right before each task is run. The renaming is best effort, if a {@link SecurityManager} 910 * prevents the renaming then it will be skipped but the tasks will still execute. 911 * 912 * @param service The executor to decorate 913 * @param nameSupplier The source of names for each task 914 */ 915 @J2ktIncompatible 916 @GwtIncompatible // concurrency 917 static ExecutorService renamingDecorator(ExecutorService service, Supplier<String> nameSupplier) { 918 checkNotNull(service); 919 checkNotNull(nameSupplier); 920 return new WrappingExecutorService(service) { 921 @Override 922 protected <T extends @Nullable Object> Callable<T> wrapTask(Callable<T> callable) { 923 return threadRenaming(callable, nameSupplier); 924 } 925 926 @Override 927 protected Runnable wrapTask(Runnable command) { 928 return threadRenaming(command, nameSupplier); 929 } 930 }; 931 } 932 933 /** 934 * Creates a {@link ScheduledExecutorService} that renames the {@link Thread threads} that its 935 * tasks run in. 936 * 937 * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed 938 * right before each task is run. The renaming is best effort, if a {@link SecurityManager} 939 * prevents the renaming then it will be skipped but the tasks will still execute. 940 * 941 * @param service The executor to decorate 942 * @param nameSupplier The source of names for each task 943 */ 944 @J2ktIncompatible 945 @GwtIncompatible // concurrency 946 static ScheduledExecutorService renamingDecorator( 947 ScheduledExecutorService service, Supplier<String> nameSupplier) { 948 checkNotNull(service); 949 checkNotNull(nameSupplier); 950 return new WrappingScheduledExecutorService(service) { 951 @Override 952 protected <T extends @Nullable Object> Callable<T> wrapTask(Callable<T> callable) { 953 return threadRenaming(callable, nameSupplier); 954 } 955 956 @Override 957 protected Runnable wrapTask(Runnable command) { 958 return threadRenaming(command, nameSupplier); 959 } 960 }; 961 } 962 963 /** 964 * Shuts down the given executor service gradually, first disabling new submissions and later, if 965 * necessary, cancelling remaining tasks. 966 * 967 * <p>The method takes the following steps: 968 * 969 * <ol> 970 * <li>calls {@link ExecutorService#shutdown()}, disabling acceptance of new submitted tasks. 971 * <li>awaits executor service termination for half of the specified timeout. 972 * <li>if the timeout expires, it calls {@link ExecutorService#shutdownNow()}, cancelling 973 * pending tasks and interrupting running tasks. 974 * <li>awaits executor service termination for the other half of the specified timeout. 975 * </ol> 976 * 977 * <p>If, at any step of the process, the calling thread is interrupted, the method calls {@link 978 * ExecutorService#shutdownNow()} and returns. 979 * 980 * <p>For a version of this method that waits <i>indefinitely</i>, use {@link 981 * ExecutorService#close}. 982 * 983 * @param service the {@code ExecutorService} to shut down 984 * @param timeout the maximum time to wait for the {@code ExecutorService} to terminate 985 * @return {@code true} if the {@code ExecutorService} was terminated successfully, {@code false} 986 * if the call timed out or was interrupted 987 * @since 28.0 (but only since 33.4.0 in the Android flavor) 988 */ 989 @CanIgnoreReturnValue 990 @J2ktIncompatible 991 @GwtIncompatible // java.time.Duration 992 public static boolean shutdownAndAwaitTermination(ExecutorService service, Duration timeout) { 993 return shutdownAndAwaitTermination(service, toNanosSaturated(timeout), NANOSECONDS); 994 } 995 996 /** 997 * Shuts down the given executor service gradually, first disabling new submissions and later, if 998 * necessary, cancelling remaining tasks. 999 * 1000 * <p>The method takes the following steps: 1001 * 1002 * <ol> 1003 * <li>calls {@link ExecutorService#shutdown()}, disabling acceptance of new submitted tasks. 1004 * <li>awaits executor service termination for half of the specified timeout. 1005 * <li>if the timeout expires, it calls {@link ExecutorService#shutdownNow()}, cancelling 1006 * pending tasks and interrupting running tasks. 1007 * <li>awaits executor service termination for the other half of the specified timeout. 1008 * </ol> 1009 * 1010 * <p>If, at any step of the process, the calling thread is interrupted, the method calls {@link 1011 * ExecutorService#shutdownNow()} and returns. 1012 * 1013 * <p>For a version of this method that waits <i>indefinitely</i>, use {@link 1014 * ExecutorService#close}. 1015 * 1016 * @param service the {@code ExecutorService} to shut down 1017 * @param timeout the maximum time to wait for the {@code ExecutorService} to terminate 1018 * @param unit the time unit of the timeout argument 1019 * @return {@code true} if the {@code ExecutorService} was terminated successfully, {@code false} 1020 * if the call timed out or was interrupted 1021 * @since 17.0 1022 */ 1023 @CanIgnoreReturnValue 1024 @J2ktIncompatible 1025 @GwtIncompatible // concurrency 1026 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 1027 public static boolean shutdownAndAwaitTermination( 1028 ExecutorService service, long timeout, TimeUnit unit) { 1029 long halfTimeoutNanos = unit.toNanos(timeout) / 2; 1030 // Disable new tasks from being submitted 1031 service.shutdown(); 1032 try { 1033 // Wait for half the duration of the timeout for existing tasks to terminate 1034 if (!service.awaitTermination(halfTimeoutNanos, NANOSECONDS)) { 1035 // Cancel currently executing tasks 1036 service.shutdownNow(); 1037 // Wait the other half of the timeout for tasks to respond to being cancelled 1038 service.awaitTermination(halfTimeoutNanos, NANOSECONDS); 1039 } 1040 } catch (InterruptedException ie) { 1041 // Preserve interrupted status 1042 Thread.currentThread().interrupt(); 1043 // (Re-)Cancel if current thread also interrupted 1044 service.shutdownNow(); 1045 } 1046 return service.isTerminated(); 1047 } 1048 1049 /** 1050 * Returns an Executor that will propagate {@link RejectedExecutionException} from the delegate 1051 * executor to the given {@code future}. 1052 * 1053 * <p>Note, the returned executor can only be used once. 1054 */ 1055 static Executor rejectionPropagatingExecutor(Executor delegate, AbstractFuture<?> future) { 1056 checkNotNull(delegate); 1057 checkNotNull(future); 1058 if (delegate == directExecutor()) { 1059 // directExecutor() cannot throw RejectedExecutionException 1060 return delegate; 1061 } 1062 return command -> { 1063 try { 1064 delegate.execute(command); 1065 } catch (RejectedExecutionException e) { 1066 future.setExceptionInternal(e); 1067 } 1068 }; 1069 } 1070}