1 7 8 package java.util.concurrent; 9 import java.util.*; 10 import java.util.concurrent.atomic.AtomicInteger ; 11 import java.security.AccessControlContext ; 12 import java.security.AccessController ; 13 import java.security.PrivilegedAction ; 14 import java.security.PrivilegedExceptionAction ; 15 import java.security.AccessControlException ; 16 17 41 public class Executors { 42 43 53 public static ExecutorService newFixedThreadPool(int nThreads) { 54 return new ThreadPoolExecutor (nThreads, nThreads, 55 0L, TimeUnit.MILLISECONDS, 56 new LinkedBlockingQueue <Runnable >()); 57 } 58 59 68 public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { 69 return new ThreadPoolExecutor (nThreads, nThreads, 70 0L, TimeUnit.MILLISECONDS, 71 new LinkedBlockingQueue <Runnable >(), 72 threadFactory); 73 } 74 75 88 public static ExecutorService newSingleThreadExecutor() { 89 return new DelegatedExecutorService 90 (new ThreadPoolExecutor (1, 1, 91 0L, TimeUnit.MILLISECONDS, 92 new LinkedBlockingQueue <Runnable >())); 93 } 94 95 108 public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { 109 return new DelegatedExecutorService 110 (new ThreadPoolExecutor (1, 1, 111 0L, TimeUnit.MILLISECONDS, 112 new LinkedBlockingQueue <Runnable >(), 113 threadFactory)); 114 } 115 116 132 public static ExecutorService newCachedThreadPool() { 133 return new ThreadPoolExecutor (0, Integer.MAX_VALUE, 134 60L, TimeUnit.SECONDS, 135 new SynchronousQueue <Runnable >()); 136 } 137 138 146 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { 147 return new ThreadPoolExecutor (0, Integer.MAX_VALUE, 148 60L, TimeUnit.SECONDS, 149 new SynchronousQueue <Runnable >(), 150 threadFactory); 151 } 152 153 166 public static ScheduledExecutorService newSingleThreadScheduledExecutor() { 167 return new DelegatedScheduledExecutorService 168 (new ScheduledThreadPoolExecutor (1)); 169 } 170 171 186 public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { 187 return new DelegatedScheduledExecutorService 188 (new ScheduledThreadPoolExecutor (1, threadFactory)); 189 } 190 191 198 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { 199 return new ScheduledThreadPoolExecutor (corePoolSize); 200 } 201 202 211 public static ScheduledExecutorService newScheduledThreadPool( 212 int corePoolSize, ThreadFactory threadFactory) { 213 return new ScheduledThreadPoolExecutor (corePoolSize, threadFactory); 214 } 215 216 217 227 public static ExecutorService unconfigurableExecutorService(ExecutorService executor) { 228 if (executor == null) 229 throw new NullPointerException (); 230 return new DelegatedExecutorService(executor); 231 } 232 233 243 public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) { 244 if (executor == null) 245 throw new NullPointerException (); 246 return new DelegatedScheduledExecutorService(executor); 247 } 248 249 264 public static ThreadFactory defaultThreadFactory() { 265 return new DefaultThreadFactory(); 266 } 267 268 300 public static ThreadFactory privilegedThreadFactory() { 301 return new PrivilegedThreadFactory(); 302 } 303 304 314 public static <T> Callable <T> callable(Runnable task, T result) { 315 if (task == null) 316 throw new NullPointerException (); 317 return new RunnableAdapter<T>(task, result); 318 } 319 320 327 public static Callable <Object > callable(Runnable task) { 328 if (task == null) 329 throw new NullPointerException (); 330 return new RunnableAdapter<Object >(task, null); 331 } 332 333 340 public static Callable <Object > callable(PrivilegedAction action) { 341 if (action == null) 342 throw new NullPointerException (); 343 return new PrivilegedActionAdapter(action); 344 } 345 346 354 public static Callable <Object > callable(PrivilegedExceptionAction action) { 355 if (action == null) 356 throw new NullPointerException (); 357 return new PrivilegedExceptionActionAdapter(action); 358 } 359 360 374 public static <T> Callable <T> privilegedCallable(Callable <T> callable) { 375 if (callable == null) 376 throw new NullPointerException (); 377 return new PrivilegedCallable(callable); 378 } 379 380 398 public static <T> Callable <T> privilegedCallableUsingCurrentClassLoader(Callable <T> callable) { 399 if (callable == null) 400 throw new NullPointerException (); 401 return new PrivilegedCallableUsingCurrentClassLoader(callable); 402 } 403 404 406 409 static final class RunnableAdapter<T> implements Callable <T> { 410 final Runnable task; 411 final T result; 412 RunnableAdapter(Runnable task, T result) { 413 this.task = task; 414 this.result = result; 415 } 416 public T call() { 417 task.run(); 418 return result; 419 } 420 } 421 422 425 static final class PrivilegedActionAdapter implements Callable <Object > { 426 PrivilegedActionAdapter(PrivilegedAction action) { 427 this.action = action; 428 } 429 public Object call () { 430 return action.run(); 431 } 432 private final PrivilegedAction action; 433 } 434 435 438 static final class PrivilegedExceptionActionAdapter implements Callable <Object > { 439 PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) { 440 this.action = action; 441 } 442 public Object call () throws Exception { 443 return action.run(); 444 } 445 private final PrivilegedExceptionAction action; 446 } 447 448 449 452 static final class PrivilegedCallable<T> implements Callable <T> { 453 private final AccessControlContext acc; 454 private final Callable <T> task; 455 private T result; 456 private Exception exception; 457 PrivilegedCallable(Callable <T> task) { 458 this.task = task; 459 this.acc = AccessController.getContext(); 460 } 461 462 public T call() throws Exception { 463 AccessController.doPrivileged(new PrivilegedAction () { 464 public Object run() { 465 try { 466 result = task.call(); 467 } catch(Exception ex) { 468 exception = ex; 469 } 470 return null; 471 } 472 }, acc); 473 if (exception != null) 474 throw exception; 475 else 476 return result; 477 } 478 } 479 480 484 static final class PrivilegedCallableUsingCurrentClassLoader<T> implements Callable <T> { 485 private final ClassLoader ccl; 486 private final AccessControlContext acc; 487 private final Callable <T> task; 488 private T result; 489 private Exception exception; 490 PrivilegedCallableUsingCurrentClassLoader(Callable <T> task) { 491 this.task = task; 492 this.ccl = Thread.currentThread().getContextClassLoader(); 493 this.acc = AccessController.getContext(); 494 acc.checkPermission(new RuntimePermission ("getContextClassLoader")); 495 acc.checkPermission(new RuntimePermission ("setContextClassLoader")); 496 } 497 498 public T call() throws Exception { 499 AccessController.doPrivileged(new PrivilegedAction () { 500 public Object run() { 501 ClassLoader savedcl = null; 502 Thread t = Thread.currentThread(); 503 try { 504 ClassLoader cl = t.getContextClassLoader(); 505 if (ccl != cl) { 506 t.setContextClassLoader(ccl); 507 savedcl = cl; 508 } 509 result = task.call(); 510 } catch(Exception ex) { 511 exception = ex; 512 } finally { 513 if (savedcl != null) 514 t.setContextClassLoader(savedcl); 515 } 516 return null; 517 } 518 }, acc); 519 if (exception != null) 520 throw exception; 521 else 522 return result; 523 } 524 } 525 526 529 static class DefaultThreadFactory implements ThreadFactory { 530 static final AtomicInteger poolNumber = new AtomicInteger (1); 531 final ThreadGroup group; 532 final AtomicInteger threadNumber = new AtomicInteger (1); 533 final String namePrefix; 534 535 DefaultThreadFactory() { 536 SecurityManager s = System.getSecurityManager(); 537 group = (s != null)? s.getThreadGroup() : 538 Thread.currentThread().getThreadGroup(); 539 namePrefix = "pool-" + 540 poolNumber.getAndIncrement() + 541 "-thread-"; 542 } 543 544 public Thread newThread(Runnable r) { 545 Thread t = new Thread (group, r, 546 namePrefix + threadNumber.getAndIncrement(), 547 0); 548 if (t.isDaemon()) 549 t.setDaemon(false); 550 if (t.getPriority() != Thread.NORM_PRIORITY) 551 t.setPriority(Thread.NORM_PRIORITY); 552 return t; 553 } 554 } 555 556 559 static class PrivilegedThreadFactory extends DefaultThreadFactory { 560 private final ClassLoader ccl; 561 private final AccessControlContext acc; 562 563 PrivilegedThreadFactory() { 564 super(); 565 this.ccl = Thread.currentThread().getContextClassLoader(); 566 this.acc = AccessController.getContext(); 567 acc.checkPermission(new RuntimePermission ("setContextClassLoader")); 568 } 569 570 public Thread newThread(final Runnable r) { 571 return super.newThread(new Runnable () { 572 public void run() { 573 AccessController.doPrivileged(new PrivilegedAction () { 574 public Object run() { 575 Thread.currentThread().setContextClassLoader(ccl); 576 r.run(); 577 return null; 578 } 579 }, acc); 580 } 581 }); 582 } 583 584 } 585 586 590 static class DelegatedExecutorService extends AbstractExecutorService { 591 private final ExecutorService e; 592 DelegatedExecutorService(ExecutorService executor) { e = executor; } 593 public void execute(Runnable command) { e.execute(command); } 594 public void shutdown() { e.shutdown(); } 595 public List<Runnable > shutdownNow() { return e.shutdownNow(); } 596 public boolean isShutdown() { return e.isShutdown(); } 597 public boolean isTerminated() { return e.isTerminated(); } 598 public boolean awaitTermination(long timeout, TimeUnit unit) 599 throws InterruptedException { 600 return e.awaitTermination(timeout, unit); 601 } 602 public Future <?> submit(Runnable task) { 603 return e.submit(task); 604 } 605 public <T> Future <T> submit(Callable <T> task) { 606 return e.submit(task); 607 } 608 public <T> Future <T> submit(Runnable task, T result) { 609 return e.submit(task, result); 610 } 611 public <T> List<Future <T>> invokeAll(Collection<Callable <T>> tasks) 612 throws InterruptedException { 613 return e.invokeAll(tasks); 614 } 615 public <T> List<Future <T>> invokeAll(Collection<Callable <T>> tasks, 616 long timeout, TimeUnit unit) 617 throws InterruptedException { 618 return e.invokeAll(tasks, timeout, unit); 619 } 620 public <T> T invokeAny(Collection<Callable <T>> tasks) 621 throws InterruptedException , ExecutionException { 622 return e.invokeAny(tasks); 623 } 624 public <T> T invokeAny(Collection<Callable <T>> tasks, 625 long timeout, TimeUnit unit) 626 throws InterruptedException , ExecutionException , TimeoutException { 627 return e.invokeAny(tasks, timeout, unit); 628 } 629 } 630 631 635 static class DelegatedScheduledExecutorService 636 extends DelegatedExecutorService 637 implements ScheduledExecutorService { 638 private final ScheduledExecutorService e; 639 DelegatedScheduledExecutorService(ScheduledExecutorService executor) { 640 super(executor); 641 e = executor; 642 } 643 public ScheduledFuture <?> schedule(Runnable command, long delay, TimeUnit unit) { 644 return e.schedule(command, delay, unit); 645 } 646 public <V> ScheduledFuture <V> schedule(Callable <V> callable, long delay, TimeUnit unit) { 647 return e.schedule(callable, delay, unit); 648 } 649 public ScheduledFuture <?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { 650 return e.scheduleAtFixedRate(command, initialDelay, period, unit); 651 } 652 public ScheduledFuture <?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { 653 return e.scheduleWithFixedDelay(command, initialDelay, delay, unit); 654 } 655 } 656 657 658 659 private Executors() {} 660 } 661 | Popular Tags |