1 /* 2 * Written by Doug Lea with assistance from members of JCP JSR-166 3 * Expert Group and released to the public domain, as explained at 4 * http://creativecommons.org/licenses/publicdomain 5 */ 6 7 package java.util.concurrent; 8 import java.util.*; 9 import java.util.concurrent.atomic.AtomicInteger; 10 import java.security.AccessControlContext; 11 import java.security.AccessController; 12 import java.security.PrivilegedAction; 13 import java.security.PrivilegedExceptionAction; 14 import java.security.PrivilegedActionException; 15 import java.security.AccessControlException; 16 // import sun.security.util.SecurityConstants; // android-removed 17 18 /** 19 * Factory and utility methods for {@link Executor}, {@link 20 * ExecutorService}, {@link ScheduledExecutorService}, {@link 21 * ThreadFactory}, and {@link Callable} classes defined in this 22 * package. This class supports the following kinds of methods: 23 * 24 * <ul> 25 * <li> Methods that create and return an {@link ExecutorService} 26 * set up with commonly useful configuration settings. 27 * <li> Methods that create and return a {@link ScheduledExecutorService} 28 * set up with commonly useful configuration settings. 29 * <li> Methods that create and return a "wrapped" ExecutorService, that 30 * disables reconfiguration by making implementation-specific methods 31 * inaccessible. 32 * <li> Methods that create and return a {@link ThreadFactory} 33 * that sets newly created threads to a known state. 34 * <li> Methods that create and return a {@link Callable} 35 * out of other closure-like forms, so they can be used 36 * in execution methods requiring <tt>Callable</tt>. 37 * </ul> 38 * 39 * @since 1.5 40 * @author Doug Lea 41 */ 42 public class Executors { 43 44 /** 45 * Creates a thread pool that reuses a fixed number of threads 46 * operating off a shared unbounded queue. At any point, at most 47 * <tt>nThreads</tt> threads will be active processing tasks. 48 * If additional tasks are submitted when all threads are active, 49 * they will wait in the queue until a thread is available. 50 * If any thread terminates due to a failure during execution 51 * prior to shutdown, a new one will take its place if needed to 52 * execute subsequent tasks. The threads in the pool will exist 53 * until it is explicitly {@link ExecutorService#shutdown shutdown}. 54 * 55 * @param nThreads the number of threads in the pool 56 * @return the newly created thread pool 57 * @throws IllegalArgumentException if {@code nThreads <= 0} 58 */ newFixedThreadPool(int nThreads)59 public static ExecutorService newFixedThreadPool(int nThreads) { 60 return new ThreadPoolExecutor(nThreads, nThreads, 61 0L, TimeUnit.MILLISECONDS, 62 new LinkedBlockingQueue<Runnable>()); 63 } 64 65 /** 66 * Creates a thread pool that reuses a fixed number of threads 67 * operating off a shared unbounded queue, using the provided 68 * ThreadFactory to create new threads when needed. At any point, 69 * at most <tt>nThreads</tt> threads will be active processing 70 * tasks. If additional tasks are submitted when all threads are 71 * active, they will wait in the queue until a thread is 72 * available. If any thread terminates due to a failure during 73 * execution prior to shutdown, a new one will take its place if 74 * needed to execute subsequent tasks. The threads in the pool will 75 * exist until it is explicitly {@link ExecutorService#shutdown 76 * shutdown}. 77 * 78 * @param nThreads the number of threads in the pool 79 * @param threadFactory the factory to use when creating new threads 80 * @return the newly created thread pool 81 * @throws NullPointerException if threadFactory is null 82 * @throws IllegalArgumentException if {@code nThreads <= 0} 83 */ newFixedThreadPool(int nThreads, ThreadFactory threadFactory)84 public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { 85 return new ThreadPoolExecutor(nThreads, nThreads, 86 0L, TimeUnit.MILLISECONDS, 87 new LinkedBlockingQueue<Runnable>(), 88 threadFactory); 89 } 90 91 /** 92 * Creates an Executor that uses a single worker thread operating 93 * off an unbounded queue. (Note however that if this single 94 * thread terminates due to a failure during execution prior to 95 * shutdown, a new one will take its place if needed to execute 96 * subsequent tasks.) Tasks are guaranteed to execute 97 * sequentially, and no more than one task will be active at any 98 * given time. Unlike the otherwise equivalent 99 * <tt>newFixedThreadPool(1)</tt> the returned executor is 100 * guaranteed not to be reconfigurable to use additional threads. 101 * 102 * @return the newly created single-threaded Executor 103 */ newSingleThreadExecutor()104 public static ExecutorService newSingleThreadExecutor() { 105 return new FinalizableDelegatedExecutorService 106 (new ThreadPoolExecutor(1, 1, 107 0L, TimeUnit.MILLISECONDS, 108 new LinkedBlockingQueue<Runnable>())); 109 } 110 111 /** 112 * Creates an Executor that uses a single worker thread operating 113 * off an unbounded queue, and uses the provided ThreadFactory to 114 * create a new thread when needed. Unlike the otherwise 115 * equivalent <tt>newFixedThreadPool(1, threadFactory)</tt> the 116 * returned executor is guaranteed not to be reconfigurable to use 117 * additional threads. 118 * 119 * @param threadFactory the factory to use when creating new 120 * threads 121 * 122 * @return the newly created single-threaded Executor 123 * @throws NullPointerException if threadFactory is null 124 */ newSingleThreadExecutor(ThreadFactory threadFactory)125 public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { 126 return new FinalizableDelegatedExecutorService 127 (new ThreadPoolExecutor(1, 1, 128 0L, TimeUnit.MILLISECONDS, 129 new LinkedBlockingQueue<Runnable>(), 130 threadFactory)); 131 } 132 133 /** 134 * Creates a thread pool that creates new threads as needed, but 135 * will reuse previously constructed threads when they are 136 * available. These pools will typically improve the performance 137 * of programs that execute many short-lived asynchronous tasks. 138 * Calls to <tt>execute</tt> will reuse previously constructed 139 * threads if available. If no existing thread is available, a new 140 * thread will be created and added to the pool. Threads that have 141 * not been used for sixty seconds are terminated and removed from 142 * the cache. Thus, a pool that remains idle for long enough will 143 * not consume any resources. Note that pools with similar 144 * properties but different details (for example, timeout parameters) 145 * may be created using {@link ThreadPoolExecutor} constructors. 146 * 147 * @return the newly created thread pool 148 */ newCachedThreadPool()149 public static ExecutorService newCachedThreadPool() { 150 return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 151 60L, TimeUnit.SECONDS, 152 new SynchronousQueue<Runnable>()); 153 } 154 155 /** 156 * Creates a thread pool that creates new threads as needed, but 157 * will reuse previously constructed threads when they are 158 * available, and uses the provided 159 * ThreadFactory to create new threads when needed. 160 * @param threadFactory the factory to use when creating new threads 161 * @return the newly created thread pool 162 * @throws NullPointerException if threadFactory is null 163 */ newCachedThreadPool(ThreadFactory threadFactory)164 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { 165 return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 166 60L, TimeUnit.SECONDS, 167 new SynchronousQueue<Runnable>(), 168 threadFactory); 169 } 170 171 /** 172 * Creates a single-threaded executor that can schedule commands 173 * to run after a given delay, or to execute periodically. 174 * (Note however that if this single 175 * thread terminates due to a failure during execution prior to 176 * shutdown, a new one will take its place if needed to execute 177 * subsequent tasks.) Tasks are guaranteed to execute 178 * sequentially, and no more than one task will be active at any 179 * given time. Unlike the otherwise equivalent 180 * <tt>newScheduledThreadPool(1)</tt> the returned executor is 181 * guaranteed not to be reconfigurable to use additional threads. 182 * @return the newly created scheduled executor 183 */ newSingleThreadScheduledExecutor()184 public static ScheduledExecutorService newSingleThreadScheduledExecutor() { 185 return new DelegatedScheduledExecutorService 186 (new ScheduledThreadPoolExecutor(1)); 187 } 188 189 /** 190 * Creates a single-threaded executor that can schedule commands 191 * to run after a given delay, or to execute periodically. (Note 192 * however that if this single thread terminates due to a failure 193 * during execution prior to shutdown, a new one will take its 194 * place if needed to execute subsequent tasks.) Tasks are 195 * guaranteed to execute sequentially, and no more than one task 196 * will be active at any given time. Unlike the otherwise 197 * equivalent <tt>newScheduledThreadPool(1, threadFactory)</tt> 198 * the returned executor is guaranteed not to be reconfigurable to 199 * use additional threads. 200 * @param threadFactory the factory to use when creating new 201 * threads 202 * @return a newly created scheduled executor 203 * @throws NullPointerException if threadFactory is null 204 */ newSingleThreadScheduledExecutor(ThreadFactory threadFactory)205 public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { 206 return new DelegatedScheduledExecutorService 207 (new ScheduledThreadPoolExecutor(1, threadFactory)); 208 } 209 210 /** 211 * Creates a thread pool that can schedule commands to run after a 212 * given delay, or to execute periodically. 213 * @param corePoolSize the number of threads to keep in the pool, 214 * even if they are idle. 215 * @return a newly created scheduled thread pool 216 * @throws IllegalArgumentException if {@code corePoolSize < 0} 217 */ newScheduledThreadPool(int corePoolSize)218 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { 219 return new ScheduledThreadPoolExecutor(corePoolSize); 220 } 221 222 /** 223 * Creates a thread pool that can schedule commands to run after a 224 * given delay, or to execute periodically. 225 * @param corePoolSize the number of threads to keep in the pool, 226 * even if they are idle. 227 * @param threadFactory the factory to use when the executor 228 * creates a new thread. 229 * @return a newly created scheduled thread pool 230 * @throws IllegalArgumentException if {@code corePoolSize < 0} 231 * @throws NullPointerException if threadFactory is null 232 */ newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory)233 public static ScheduledExecutorService newScheduledThreadPool( 234 int corePoolSize, ThreadFactory threadFactory) { 235 return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); 236 } 237 238 239 /** 240 * Returns an object that delegates all defined {@link 241 * ExecutorService} methods to the given executor, but not any 242 * other methods that might otherwise be accessible using 243 * casts. This provides a way to safely "freeze" configuration and 244 * disallow tuning of a given concrete implementation. 245 * @param executor the underlying implementation 246 * @return an <tt>ExecutorService</tt> instance 247 * @throws NullPointerException if executor null 248 */ unconfigurableExecutorService(ExecutorService executor)249 public static ExecutorService unconfigurableExecutorService(ExecutorService executor) { 250 if (executor == null) 251 throw new NullPointerException(); 252 return new DelegatedExecutorService(executor); 253 } 254 255 /** 256 * Returns an object that delegates all defined {@link 257 * ScheduledExecutorService} methods to the given executor, but 258 * not any other methods that might otherwise be accessible using 259 * casts. This provides a way to safely "freeze" configuration and 260 * disallow tuning of a given concrete implementation. 261 * @param executor the underlying implementation 262 * @return a <tt>ScheduledExecutorService</tt> instance 263 * @throws NullPointerException if executor null 264 */ unconfigurableScheduledExecutorService(ScheduledExecutorService executor)265 public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) { 266 if (executor == null) 267 throw new NullPointerException(); 268 return new DelegatedScheduledExecutorService(executor); 269 } 270 271 /** 272 * Returns a default thread factory used to create new threads. 273 * This factory creates all new threads used by an Executor in the 274 * same {@link ThreadGroup}. If there is a {@link 275 * java.lang.SecurityManager}, it uses the group of {@link 276 * System#getSecurityManager}, else the group of the thread 277 * invoking this <tt>defaultThreadFactory</tt> method. Each new 278 * thread is created as a non-daemon thread with priority set to 279 * the smaller of <tt>Thread.NORM_PRIORITY</tt> and the maximum 280 * priority permitted in the thread group. New threads have names 281 * accessible via {@link Thread#getName} of 282 * <em>pool-N-thread-M</em>, where <em>N</em> is the sequence 283 * number of this factory, and <em>M</em> is the sequence number 284 * of the thread created by this factory. 285 * @return a thread factory 286 */ defaultThreadFactory()287 public static ThreadFactory defaultThreadFactory() { 288 return new DefaultThreadFactory(); 289 } 290 291 /** 292 * Returns a thread factory used to create new threads that 293 * have the same permissions as the current thread. 294 * This factory creates threads with the same settings as {@link 295 * Executors#defaultThreadFactory}, additionally setting the 296 * AccessControlContext and contextClassLoader of new threads to 297 * be the same as the thread invoking this 298 * <tt>privilegedThreadFactory</tt> method. A new 299 * <tt>privilegedThreadFactory</tt> can be created within an 300 * {@link AccessController#doPrivileged} action setting the 301 * current thread's access control context to create threads with 302 * the selected permission settings holding within that action. 303 * 304 * <p> Note that while tasks running within such threads will have 305 * the same access control and class loader settings as the 306 * current thread, they need not have the same {@link 307 * java.lang.ThreadLocal} or {@link 308 * java.lang.InheritableThreadLocal} values. If necessary, 309 * particular values of thread locals can be set or reset before 310 * any task runs in {@link ThreadPoolExecutor} subclasses using 311 * {@link ThreadPoolExecutor#beforeExecute}. Also, if it is 312 * necessary to initialize worker threads to have the same 313 * InheritableThreadLocal settings as some other designated 314 * thread, you can create a custom ThreadFactory in which that 315 * thread waits for and services requests to create others that 316 * will inherit its values. 317 * 318 * @return a thread factory 319 * @throws AccessControlException if the current access control 320 * context does not have permission to both get and set context 321 * class loader. 322 */ privilegedThreadFactory()323 public static ThreadFactory privilegedThreadFactory() { 324 return new PrivilegedThreadFactory(); 325 } 326 327 /** 328 * Returns a {@link Callable} object that, when 329 * called, runs the given task and returns the given result. This 330 * can be useful when applying methods requiring a 331 * <tt>Callable</tt> to an otherwise resultless action. 332 * @param task the task to run 333 * @param result the result to return 334 * @return a callable object 335 * @throws NullPointerException if task null 336 */ callable(Runnable task, T result)337 public static <T> Callable<T> callable(Runnable task, T result) { 338 if (task == null) 339 throw new NullPointerException(); 340 return new RunnableAdapter<T>(task, result); 341 } 342 343 /** 344 * Returns a {@link Callable} object that, when 345 * called, runs the given task and returns <tt>null</tt>. 346 * @param task the task to run 347 * @return a callable object 348 * @throws NullPointerException if task null 349 */ callable(Runnable task)350 public static Callable<Object> callable(Runnable task) { 351 if (task == null) 352 throw new NullPointerException(); 353 return new RunnableAdapter<Object>(task, null); 354 } 355 356 /** 357 * Returns a {@link Callable} object that, when 358 * called, runs the given privileged action and returns its result. 359 * @param action the privileged action to run 360 * @return a callable object 361 * @throws NullPointerException if action null 362 */ callable(final PrivilegedAction<?> action)363 public static Callable<Object> callable(final PrivilegedAction<?> action) { 364 if (action == null) 365 throw new NullPointerException(); 366 return new Callable<Object>() { 367 public Object call() { return action.run(); }}; 368 } 369 370 /** 371 * Returns a {@link Callable} object that, when 372 * called, runs the given privileged exception action and returns 373 * its result. 374 * @param action the privileged exception action to run 375 * @return a callable object 376 * @throws NullPointerException if action null 377 */ 378 public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) { 379 if (action == null) 380 throw new NullPointerException(); 381 return new Callable<Object>() { 382 public Object call() throws Exception { return action.run(); }}; 383 } 384 385 /** 386 * Returns a {@link Callable} object that will, when 387 * called, execute the given <tt>callable</tt> under the current 388 * access control context. This method should normally be 389 * invoked within an {@link AccessController#doPrivileged} action 390 * to create callables that will, if possible, execute under the 391 * selected permission settings holding within that action; or if 392 * not possible, throw an associated {@link 393 * AccessControlException}. 394 * @param callable the underlying task 395 * @return a callable object 396 * @throws NullPointerException if callable null 397 * 398 */ 399 public static <T> Callable<T> privilegedCallable(Callable<T> callable) { 400 if (callable == null) 401 throw new NullPointerException(); 402 return new PrivilegedCallable<T>(callable); 403 } 404 405 /** 406 * Returns a {@link Callable} object that will, when 407 * called, execute the given <tt>callable</tt> under the current 408 * access control context, with the current context class loader 409 * as the context class loader. This method should normally be 410 * invoked within an {@link AccessController#doPrivileged} action 411 * to create callables that will, if possible, execute under the 412 * selected permission settings holding within that action; or if 413 * not possible, throw an associated {@link 414 * AccessControlException}. 415 * @param callable the underlying task 416 * 417 * @return a callable object 418 * @throws NullPointerException if callable null 419 * @throws AccessControlException if the current access control 420 * context does not have permission to both set and get context 421 * class loader. 422 */ 423 public static <T> Callable<T> privilegedCallableUsingCurrentClassLoader(Callable<T> callable) { 424 if (callable == null) 425 throw new NullPointerException(); 426 return new PrivilegedCallableUsingCurrentClassLoader<T>(callable); 427 } 428 429 // Non-public classes supporting the public methods 430 431 /** 432 * A callable that runs given task and returns given result 433 */ 434 static final class RunnableAdapter<T> implements Callable<T> { 435 final Runnable task; 436 final T result; 437 RunnableAdapter(Runnable task, T result) { 438 this.task = task; 439 this.result = result; 440 } 441 public T call() { 442 task.run(); 443 return result; 444 } 445 } 446 447 /** 448 * A callable that runs under established access control settings 449 */ 450 static final class PrivilegedCallable<T> implements Callable<T> { 451 private final Callable<T> task; 452 private final AccessControlContext acc; 453 454 PrivilegedCallable(Callable<T> task) { 455 this.task = task; 456 this.acc = AccessController.getContext(); 457 } 458 459 public T call() throws Exception { 460 try { 461 return AccessController.doPrivileged( 462 new PrivilegedExceptionAction<T>() { 463 public T run() throws Exception { 464 return task.call(); 465 } 466 }, acc); 467 } catch (PrivilegedActionException e) { 468 throw e.getException(); 469 } 470 } 471 } 472 473 /** 474 * A callable that runs under established access control settings and 475 * current ClassLoader 476 */ 477 static final class PrivilegedCallableUsingCurrentClassLoader<T> implements Callable<T> { 478 private final Callable<T> task; 479 private final AccessControlContext acc; 480 private final ClassLoader ccl; 481 482 PrivilegedCallableUsingCurrentClassLoader(Callable<T> task) { 483 SecurityManager sm = System.getSecurityManager(); 484 if (sm != null) { 485 // Calls to getContextClassLoader from this class 486 // never trigger a security check, but we check 487 // whether our callers have this permission anyways. 488 sm.checkPermission(new RuntimePermission("getContextClassLoader")); // android-changed 489 490 // Whether setContextClassLoader turns out to be necessary 491 // or not, we fail fast if permission is not available. 492 sm.checkPermission(new RuntimePermission("setContextClassLoader")); 493 } 494 this.task = task; 495 this.acc = AccessController.getContext(); 496 this.ccl = Thread.currentThread().getContextClassLoader(); 497 } 498 499 public T call() throws Exception { 500 try { 501 return AccessController.doPrivileged( 502 new PrivilegedExceptionAction<T>() { 503 public T run() throws Exception { 504 ClassLoader savedcl = null; 505 Thread t = Thread.currentThread(); 506 try { 507 ClassLoader cl = t.getContextClassLoader(); 508 if (ccl != cl) { 509 t.setContextClassLoader(ccl); 510 savedcl = cl; 511 } 512 return task.call(); 513 } finally { 514 if (savedcl != null) 515 t.setContextClassLoader(savedcl); 516 } 517 } 518 }, acc); 519 } catch (PrivilegedActionException e) { 520 throw e.getException(); 521 } 522 } 523 } 524 525 /** 526 * The default thread factory 527 */ 528 static class DefaultThreadFactory implements ThreadFactory { 529 private static final AtomicInteger poolNumber = new AtomicInteger(1); 530 private final ThreadGroup group; 531 private final AtomicInteger threadNumber = new AtomicInteger(1); 532 private final String namePrefix; 533 534 DefaultThreadFactory() { 535 SecurityManager s = System.getSecurityManager(); 536 group = (s != null) ? s.getThreadGroup() : 537 Thread.currentThread().getThreadGroup(); 538 namePrefix = "pool-" + 539 poolNumber.getAndIncrement() + 540 "-thread-"; 541 } 542 543 public Thread newThread(Runnable r) { 544 Thread t = new Thread(group, r, 545 namePrefix + threadNumber.getAndIncrement(), 546 0); 547 if (t.isDaemon()) 548 t.setDaemon(false); 549 if (t.getPriority() != Thread.NORM_PRIORITY) 550 t.setPriority(Thread.NORM_PRIORITY); 551 return t; 552 } 553 } 554 555 /** 556 * Thread factory capturing access control context and class loader 557 */ 558 static class PrivilegedThreadFactory extends DefaultThreadFactory { 559 private final AccessControlContext acc; 560 private final ClassLoader ccl; 561 562 PrivilegedThreadFactory() { 563 super(); 564 SecurityManager sm = System.getSecurityManager(); 565 if (sm != null) { 566 // Calls to getContextClassLoader from this class 567 // never trigger a security check, but we check 568 // whether our callers have this permission anyways. 569 sm.checkPermission(new RuntimePermission("getContextClassLoader")); // android-changed 570 571 // Fail fast 572 sm.checkPermission(new RuntimePermission("setContextClassLoader")); 573 } 574 this.acc = AccessController.getContext(); 575 this.ccl = Thread.currentThread().getContextClassLoader(); 576 } 577 578 public Thread newThread(final Runnable r) { 579 return super.newThread(new Runnable() { 580 public void run() { 581 AccessController.doPrivileged(new PrivilegedAction<Void>() { 582 public Void run() { 583 Thread.currentThread().setContextClassLoader(ccl); 584 r.run(); 585 return null; 586 } 587 }, acc); 588 } 589 }); 590 } 591 } 592 593 /** 594 * A wrapper class that exposes only the ExecutorService methods 595 * of an ExecutorService implementation. 596 */ 597 static class DelegatedExecutorService extends AbstractExecutorService { 598 private final ExecutorService e; 599 DelegatedExecutorService(ExecutorService executor) { e = executor; } 600 public void execute(Runnable command) { e.execute(command); } 601 public void shutdown() { e.shutdown(); } 602 public List<Runnable> shutdownNow() { return e.shutdownNow(); } 603 public boolean isShutdown() { return e.isShutdown(); } 604 public boolean isTerminated() { return e.isTerminated(); } 605 public boolean awaitTermination(long timeout, TimeUnit unit) 606 throws InterruptedException { 607 return e.awaitTermination(timeout, unit); 608 } 609 public Future<?> submit(Runnable task) { 610 return e.submit(task); 611 } 612 public <T> Future<T> submit(Callable<T> task) { 613 return e.submit(task); 614 } 615 public <T> Future<T> submit(Runnable task, T result) { 616 return e.submit(task, result); 617 } 618 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) 619 throws InterruptedException { 620 return e.invokeAll(tasks); 621 } 622 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, 623 long timeout, TimeUnit unit) 624 throws InterruptedException { 625 return e.invokeAll(tasks, timeout, unit); 626 } 627 public <T> T invokeAny(Collection<? extends Callable<T>> tasks) 628 throws InterruptedException, ExecutionException { 629 return e.invokeAny(tasks); 630 } 631 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, 632 long timeout, TimeUnit unit) 633 throws InterruptedException, ExecutionException, TimeoutException { 634 return e.invokeAny(tasks, timeout, unit); 635 } 636 } 637 638 static class FinalizableDelegatedExecutorService 639 extends DelegatedExecutorService { 640 FinalizableDelegatedExecutorService(ExecutorService executor) { 641 super(executor); 642 } 643 protected void finalize() { 644 super.shutdown(); 645 } 646 } 647 648 /** 649 * A wrapper class that exposes only the ScheduledExecutorService 650 * methods of a ScheduledExecutorService implementation. 651 */ 652 static class DelegatedScheduledExecutorService 653 extends DelegatedExecutorService 654 implements ScheduledExecutorService { 655 private final ScheduledExecutorService e; 656 DelegatedScheduledExecutorService(ScheduledExecutorService executor) { 657 super(executor); 658 e = executor; 659 } 660 public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { 661 return e.schedule(command, delay, unit); 662 } 663 public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { 664 return e.schedule(callable, delay, unit); 665 } 666 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { 667 return e.scheduleAtFixedRate(command, initialDelay, period, unit); 668 } 669 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { 670 return e.scheduleWithFixedDelay(command, initialDelay, delay, unit); 671 } 672 } 673 674 675 /** Cannot instantiate. */ 676 private Executors() {} 677 } 678