< prev index next >

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

Print this page

 28  * However, the following notice accompanied the original version of this
 29  * file:
 30  *
 31  * Written by Doug Lea with assistance from members of JCP JSR-166
 32  * Expert Group and released to the public domain, as explained at
 33  * http://creativecommons.org/publicdomain/zero/1.0/
 34  */
 35 
 36 package java.util.concurrent;
 37 
 38 import static java.lang.ref.Reference.reachabilityFence;
 39 import java.security.AccessControlContext;
 40 import java.security.AccessControlException;
 41 import java.security.AccessController;
 42 import java.security.PrivilegedAction;
 43 import java.security.PrivilegedActionException;
 44 import java.security.PrivilegedExceptionAction;
 45 import java.util.Collection;
 46 import java.util.List;
 47 import java.util.concurrent.atomic.AtomicInteger;

 48 import sun.security.util.SecurityConstants;
 49 
 50 /**
 51  * Factory and utility methods for {@link Executor}, {@link
 52  * ExecutorService}, {@link ScheduledExecutorService}, {@link
 53  * ThreadFactory}, and {@link Callable} classes defined in this
 54  * package. This class supports the following kinds of methods:
 55  *
 56  * <ul>
 57  *   <li>Methods that create and return an {@link ExecutorService}
 58  *       set up with commonly useful configuration settings.
 59  *   <li>Methods that create and return a {@link ScheduledExecutorService}
 60  *       set up with commonly useful configuration settings.
 61  *   <li>Methods that create and return a "wrapped" ExecutorService, that
 62  *       disables reconfiguration by making implementation-specific methods
 63  *       inaccessible.
 64  *   <li>Methods that create and return a {@link ThreadFactory}
 65  *       that sets newly created threads to a known state.
 66  *   <li>Methods that create and return a {@link Callable}
 67  *       out of other closure-like forms, so they can be used

220                                       new SynchronousQueue<Runnable>());
221     }
222 
223     /**
224      * Creates a thread pool that creates new threads as needed, but
225      * will reuse previously constructed threads when they are
226      * available, and uses the provided
227      * ThreadFactory to create new threads when needed.
228      *
229      * @param threadFactory the factory to use when creating new threads
230      * @return the newly created thread pool
231      * @throws NullPointerException if threadFactory is null
232      */
233     public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
234         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
235                                       60L, TimeUnit.SECONDS,
236                                       new SynchronousQueue<Runnable>(),
237                                       threadFactory);
238     }
239 




































240     /**
241      * Creates a single-threaded executor that can schedule commands
242      * to run after a given delay, or to execute periodically.
243      * (Note however that if this single
244      * thread terminates due to a failure during execution prior to
245      * shutdown, a new one will take its place if needed to execute
246      * subsequent tasks.)  Tasks are guaranteed to execute
247      * sequentially, and no more than one task will be active at any
248      * given time. Unlike the otherwise equivalent
249      * {@code newScheduledThreadPool(1)} the returned executor is
250      * guaranteed not to be reconfigurable to use additional threads.
251      *
252      * @return the newly created scheduled executor
253      */
254     public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
255         return new DelegatedScheduledExecutorService
256             (new ScheduledThreadPoolExecutor(1));
257     }
258 
259     /**

 28  * However, the following notice accompanied the original version of this
 29  * file:
 30  *
 31  * Written by Doug Lea with assistance from members of JCP JSR-166
 32  * Expert Group and released to the public domain, as explained at
 33  * http://creativecommons.org/publicdomain/zero/1.0/
 34  */
 35 
 36 package java.util.concurrent;
 37 
 38 import static java.lang.ref.Reference.reachabilityFence;
 39 import java.security.AccessControlContext;
 40 import java.security.AccessControlException;
 41 import java.security.AccessController;
 42 import java.security.PrivilegedAction;
 43 import java.security.PrivilegedActionException;
 44 import java.security.PrivilegedExceptionAction;
 45 import java.util.Collection;
 46 import java.util.List;
 47 import java.util.concurrent.atomic.AtomicInteger;
 48 import jdk.internal.javac.PreviewFeature;
 49 import sun.security.util.SecurityConstants;
 50 
 51 /**
 52  * Factory and utility methods for {@link Executor}, {@link
 53  * ExecutorService}, {@link ScheduledExecutorService}, {@link
 54  * ThreadFactory}, and {@link Callable} classes defined in this
 55  * package. This class supports the following kinds of methods:
 56  *
 57  * <ul>
 58  *   <li>Methods that create and return an {@link ExecutorService}
 59  *       set up with commonly useful configuration settings.
 60  *   <li>Methods that create and return a {@link ScheduledExecutorService}
 61  *       set up with commonly useful configuration settings.
 62  *   <li>Methods that create and return a "wrapped" ExecutorService, that
 63  *       disables reconfiguration by making implementation-specific methods
 64  *       inaccessible.
 65  *   <li>Methods that create and return a {@link ThreadFactory}
 66  *       that sets newly created threads to a known state.
 67  *   <li>Methods that create and return a {@link Callable}
 68  *       out of other closure-like forms, so they can be used

221                                       new SynchronousQueue<Runnable>());
222     }
223 
224     /**
225      * Creates a thread pool that creates new threads as needed, but
226      * will reuse previously constructed threads when they are
227      * available, and uses the provided
228      * ThreadFactory to create new threads when needed.
229      *
230      * @param threadFactory the factory to use when creating new threads
231      * @return the newly created thread pool
232      * @throws NullPointerException if threadFactory is null
233      */
234     public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
235         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
236                                       60L, TimeUnit.SECONDS,
237                                       new SynchronousQueue<Runnable>(),
238                                       threadFactory);
239     }
240 
241     /**
242      * Creates an Executor that starts a new Thread for each task.
243      * The number of threads created by the Executor is unbounded.
244      *
245      * <p> Invoking {@link Future#cancel(boolean) cancel(true)} on a {@link
246      * Future Future} representing the pending result of a task submitted to
247      * the Executor will {@link Thread#interrupt() interrupt} the thread
248      * executing the task.
249      *
250      * @param threadFactory the factory to use when creating new threads
251      * @return a new executor that creates a new Thread for each task
252      * @throws NullPointerException if threadFactory is null
253      * @since 99
254      */
255     @PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)
256     public static ExecutorService newThreadPerTaskExecutor(ThreadFactory threadFactory) {
257         return new ThreadPerTaskExecutor(threadFactory);
258     }
259 
260     /**
261      * Creates an Executor that starts a new virtual Thread for each task.
262      * The number of threads created by the Executor is unbounded.
263      *
264      * <p> This method is equivalent to invoking
265      * {@link #newThreadPerTaskExecutor(ThreadFactory)} with a thread factory
266      * that creates virtual threads.
267      *
268      * @return a new executor that creates a new virtual Thread for each task
269      * @since 99
270      */
271     @PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)
272     public static ExecutorService newVirtualThreadPerTaskExecutor() {
273         ThreadFactory factory = Thread.ofVirtual().factory();
274         return newThreadPerTaskExecutor(factory);
275     }
276 
277     /**
278      * Creates a single-threaded executor that can schedule commands
279      * to run after a given delay, or to execute periodically.
280      * (Note however that if this single
281      * thread terminates due to a failure during execution prior to
282      * shutdown, a new one will take its place if needed to execute
283      * subsequent tasks.)  Tasks are guaranteed to execute
284      * sequentially, and no more than one task will be active at any
285      * given time. Unlike the otherwise equivalent
286      * {@code newScheduledThreadPool(1)} the returned executor is
287      * guaranteed not to be reconfigurable to use additional threads.
288      *
289      * @return the newly created scheduled executor
290      */
291     public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
292         return new DelegatedScheduledExecutorService
293             (new ScheduledThreadPoolExecutor(1));
294     }
295 
296     /**
< prev index next >