src/share/classes/java/util/concurrent/Executors.java

Print this page




  66  * </ul>
  67  *
  68  * @since 1.5
  69  * @author Doug Lea
  70  */
  71 public class Executors {
  72 
  73     /**
  74      * Creates a thread pool that reuses a fixed number of threads
  75      * operating off a shared unbounded queue.  At any point, at most
  76      * <tt>nThreads</tt> threads will be active processing tasks.
  77      * If additional tasks are submitted when all threads are active,
  78      * they will wait in the queue until a thread is available.
  79      * If any thread terminates due to a failure during execution
  80      * prior to shutdown, a new one will take its place if needed to
  81      * execute subsequent tasks.  The threads in the pool will exist
  82      * until it is explicitly {@link ExecutorService#shutdown shutdown}.
  83      *
  84      * @param nThreads the number of threads in the pool
  85      * @return the newly created thread pool
  86      * @throws IllegalArgumentException if <tt>nThreads &lt;= 0</tt>
  87      */
  88     public static ExecutorService newFixedThreadPool(int nThreads) {
  89         return new ThreadPoolExecutor(nThreads, nThreads,
  90                                       0L, TimeUnit.MILLISECONDS,
  91                                       new LinkedBlockingQueue<Runnable>());
  92     }
  93 
  94     /**
  95      * Creates a thread pool that reuses a fixed number of threads
  96      * operating off a shared unbounded queue, using the provided
  97      * ThreadFactory to create new threads when needed.  At any point,
  98      * at most <tt>nThreads</tt> threads will be active processing
  99      * tasks.  If additional tasks are submitted when all threads are
 100      * active, they will wait in the queue until a thread is
 101      * available.  If any thread terminates due to a failure during
 102      * execution prior to shutdown, a new one will take its place if
 103      * needed to execute subsequent tasks.  The threads in the pool will
 104      * exist until it is explicitly {@link ExecutorService#shutdown
 105      * shutdown}.
 106      *
 107      * @param nThreads the number of threads in the pool
 108      * @param threadFactory the factory to use when creating new threads
 109      * @return the newly created thread pool
 110      * @throws NullPointerException if threadFactory is null
 111      * @throws IllegalArgumentException if <tt>nThreads &lt;= 0</tt>
 112      */
 113     public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
 114         return new ThreadPoolExecutor(nThreads, nThreads,
 115                                       0L, TimeUnit.MILLISECONDS,
 116                                       new LinkedBlockingQueue<Runnable>(),
 117                                       threadFactory);
 118     }
 119 
 120     /**
 121      * Creates an Executor that uses a single worker thread operating
 122      * off an unbounded queue. (Note however that if this single
 123      * thread terminates due to a failure during execution prior to
 124      * shutdown, a new one will take its place if needed to execute
 125      * subsequent tasks.)  Tasks are guaranteed to execute
 126      * sequentially, and no more than one task will be active at any
 127      * given time. Unlike the otherwise equivalent
 128      * <tt>newFixedThreadPool(1)</tt> the returned executor is
 129      * guaranteed not to be reconfigurable to use additional threads.
 130      *
 131      * @return the newly created single-threaded Executor


 225      * will be active at any given time. Unlike the otherwise
 226      * equivalent <tt>newScheduledThreadPool(1, threadFactory)</tt>
 227      * the returned executor is guaranteed not to be reconfigurable to
 228      * use additional threads.
 229      * @param threadFactory the factory to use when creating new
 230      * threads
 231      * @return a newly created scheduled executor
 232      * @throws NullPointerException if threadFactory is null
 233      */
 234     public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
 235         return new DelegatedScheduledExecutorService
 236             (new ScheduledThreadPoolExecutor(1, threadFactory));
 237     }
 238 
 239     /**
 240      * Creates a thread pool that can schedule commands to run after a
 241      * given delay, or to execute periodically.
 242      * @param corePoolSize the number of threads to keep in the pool,
 243      * even if they are idle.
 244      * @return a newly created scheduled thread pool
 245      * @throws IllegalArgumentException if <tt>corePoolSize &lt; 0</tt>
 246      */
 247     public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
 248         return new ScheduledThreadPoolExecutor(corePoolSize);
 249     }
 250 
 251     /**
 252      * Creates a thread pool that can schedule commands to run after a
 253      * given delay, or to execute periodically.
 254      * @param corePoolSize the number of threads to keep in the pool,
 255      * even if they are idle.
 256      * @param threadFactory the factory to use when the executor
 257      * creates a new thread.
 258      * @return a newly created scheduled thread pool
 259      * @throws IllegalArgumentException if <tt>corePoolSize &lt; 0</tt>
 260      * @throws NullPointerException if threadFactory is null
 261      */
 262     public static ScheduledExecutorService newScheduledThreadPool(
 263             int corePoolSize, ThreadFactory threadFactory) {
 264         return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
 265     }
 266 
 267 
 268     /**
 269      * Returns an object that delegates all defined {@link
 270      * ExecutorService} methods to the given executor, but not any
 271      * other methods that might otherwise be accessible using
 272      * casts. This provides a way to safely "freeze" configuration and
 273      * disallow tuning of a given concrete implementation.
 274      * @param executor the underlying implementation
 275      * @return an <tt>ExecutorService</tt> instance
 276      * @throws NullPointerException if executor null
 277      */
 278     public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {
 279         if (executor == null)


 545                             }
 546                         }
 547                     }, acc);
 548             } catch (PrivilegedActionException e) {
 549                 throw e.getException();
 550             }
 551         }
 552     }
 553 
 554     /**
 555      * The default thread factory
 556      */
 557     static class DefaultThreadFactory implements ThreadFactory {
 558         private static final AtomicInteger poolNumber = new AtomicInteger(1);
 559         private final ThreadGroup group;
 560         private final AtomicInteger threadNumber = new AtomicInteger(1);
 561         private final String namePrefix;
 562 
 563         DefaultThreadFactory() {
 564             SecurityManager s = System.getSecurityManager();
 565             group = (s != null)? s.getThreadGroup() :
 566                                  Thread.currentThread().getThreadGroup();
 567             namePrefix = "pool-" +
 568                           poolNumber.getAndIncrement() +
 569                          "-thread-";
 570         }
 571 
 572         public Thread newThread(Runnable r) {
 573             Thread t = new Thread(group, r,
 574                                   namePrefix + threadNumber.getAndIncrement(),
 575                                   0);
 576             if (t.isDaemon())
 577                 t.setDaemon(false);
 578             if (t.getPriority() != Thread.NORM_PRIORITY)
 579                 t.setPriority(Thread.NORM_PRIORITY);
 580             return t;
 581         }
 582     }
 583 
 584     /**
 585      * Thread factory capturing access control context and class loader




  66  * </ul>
  67  *
  68  * @since 1.5
  69  * @author Doug Lea
  70  */
  71 public class Executors {
  72 
  73     /**
  74      * Creates a thread pool that reuses a fixed number of threads
  75      * operating off a shared unbounded queue.  At any point, at most
  76      * <tt>nThreads</tt> threads will be active processing tasks.
  77      * If additional tasks are submitted when all threads are active,
  78      * they will wait in the queue until a thread is available.
  79      * If any thread terminates due to a failure during execution
  80      * prior to shutdown, a new one will take its place if needed to
  81      * execute subsequent tasks.  The threads in the pool will exist
  82      * until it is explicitly {@link ExecutorService#shutdown shutdown}.
  83      *
  84      * @param nThreads the number of threads in the pool
  85      * @return the newly created thread pool
  86      * @throws IllegalArgumentException if {@code nThreads <= 0}
  87      */
  88     public static ExecutorService newFixedThreadPool(int nThreads) {
  89         return new ThreadPoolExecutor(nThreads, nThreads,
  90                                       0L, TimeUnit.MILLISECONDS,
  91                                       new LinkedBlockingQueue<Runnable>());
  92     }
  93 
  94     /**
  95      * Creates a thread pool that reuses a fixed number of threads
  96      * operating off a shared unbounded queue, using the provided
  97      * ThreadFactory to create new threads when needed.  At any point,
  98      * at most <tt>nThreads</tt> threads will be active processing
  99      * tasks.  If additional tasks are submitted when all threads are
 100      * active, they will wait in the queue until a thread is
 101      * available.  If any thread terminates due to a failure during
 102      * execution prior to shutdown, a new one will take its place if
 103      * needed to execute subsequent tasks.  The threads in the pool will
 104      * exist until it is explicitly {@link ExecutorService#shutdown
 105      * shutdown}.
 106      *
 107      * @param nThreads the number of threads in the pool
 108      * @param threadFactory the factory to use when creating new threads
 109      * @return the newly created thread pool
 110      * @throws NullPointerException if threadFactory is null
 111      * @throws IllegalArgumentException if {@code nThreads <= 0}
 112      */
 113     public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
 114         return new ThreadPoolExecutor(nThreads, nThreads,
 115                                       0L, TimeUnit.MILLISECONDS,
 116                                       new LinkedBlockingQueue<Runnable>(),
 117                                       threadFactory);
 118     }
 119 
 120     /**
 121      * Creates an Executor that uses a single worker thread operating
 122      * off an unbounded queue. (Note however that if this single
 123      * thread terminates due to a failure during execution prior to
 124      * shutdown, a new one will take its place if needed to execute
 125      * subsequent tasks.)  Tasks are guaranteed to execute
 126      * sequentially, and no more than one task will be active at any
 127      * given time. Unlike the otherwise equivalent
 128      * <tt>newFixedThreadPool(1)</tt> the returned executor is
 129      * guaranteed not to be reconfigurable to use additional threads.
 130      *
 131      * @return the newly created single-threaded Executor


 225      * will be active at any given time. Unlike the otherwise
 226      * equivalent <tt>newScheduledThreadPool(1, threadFactory)</tt>
 227      * the returned executor is guaranteed not to be reconfigurable to
 228      * use additional threads.
 229      * @param threadFactory the factory to use when creating new
 230      * threads
 231      * @return a newly created scheduled executor
 232      * @throws NullPointerException if threadFactory is null
 233      */
 234     public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
 235         return new DelegatedScheduledExecutorService
 236             (new ScheduledThreadPoolExecutor(1, threadFactory));
 237     }
 238 
 239     /**
 240      * Creates a thread pool that can schedule commands to run after a
 241      * given delay, or to execute periodically.
 242      * @param corePoolSize the number of threads to keep in the pool,
 243      * even if they are idle.
 244      * @return a newly created scheduled thread pool
 245      * @throws IllegalArgumentException if {@code corePoolSize < 0}
 246      */
 247     public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
 248         return new ScheduledThreadPoolExecutor(corePoolSize);
 249     }
 250 
 251     /**
 252      * Creates a thread pool that can schedule commands to run after a
 253      * given delay, or to execute periodically.
 254      * @param corePoolSize the number of threads to keep in the pool,
 255      * even if they are idle.
 256      * @param threadFactory the factory to use when the executor
 257      * creates a new thread.
 258      * @return a newly created scheduled thread pool
 259      * @throws IllegalArgumentException if {@code corePoolSize < 0}
 260      * @throws NullPointerException if threadFactory is null
 261      */
 262     public static ScheduledExecutorService newScheduledThreadPool(
 263             int corePoolSize, ThreadFactory threadFactory) {
 264         return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
 265     }
 266 
 267 
 268     /**
 269      * Returns an object that delegates all defined {@link
 270      * ExecutorService} methods to the given executor, but not any
 271      * other methods that might otherwise be accessible using
 272      * casts. This provides a way to safely "freeze" configuration and
 273      * disallow tuning of a given concrete implementation.
 274      * @param executor the underlying implementation
 275      * @return an <tt>ExecutorService</tt> instance
 276      * @throws NullPointerException if executor null
 277      */
 278     public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {
 279         if (executor == null)


 545                             }
 546                         }
 547                     }, acc);
 548             } catch (PrivilegedActionException e) {
 549                 throw e.getException();
 550             }
 551         }
 552     }
 553 
 554     /**
 555      * The default thread factory
 556      */
 557     static class DefaultThreadFactory implements ThreadFactory {
 558         private static final AtomicInteger poolNumber = new AtomicInteger(1);
 559         private final ThreadGroup group;
 560         private final AtomicInteger threadNumber = new AtomicInteger(1);
 561         private final String namePrefix;
 562 
 563         DefaultThreadFactory() {
 564             SecurityManager s = System.getSecurityManager();
 565             group = (s != null) ? s.getThreadGroup() :
 566                                   Thread.currentThread().getThreadGroup();
 567             namePrefix = "pool-" +
 568                           poolNumber.getAndIncrement() +
 569                          "-thread-";
 570         }
 571 
 572         public Thread newThread(Runnable r) {
 573             Thread t = new Thread(group, r,
 574                                   namePrefix + threadNumber.getAndIncrement(),
 575                                   0);
 576             if (t.isDaemon())
 577                 t.setDaemon(false);
 578             if (t.getPriority() != Thread.NORM_PRIORITY)
 579                 t.setPriority(Thread.NORM_PRIORITY);
 580             return t;
 581         }
 582     }
 583 
 584     /**
 585      * Thread factory capturing access control context and class loader