< prev index next >

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

Print this page
@@ -43,10 +43,11 @@
  import java.security.PrivilegedActionException;
  import java.security.PrivilegedExceptionAction;
  import java.util.Collection;
  import java.util.List;
  import java.util.concurrent.atomic.AtomicInteger;
+ import jdk.internal.javac.PreviewFeature;
  import sun.security.util.SecurityConstants;
  
  /**
   * Factory and utility methods for {@link Executor}, {@link
   * ExecutorService}, {@link ScheduledExecutorService}, {@link

@@ -235,10 +236,46 @@
                                        60L, TimeUnit.SECONDS,
                                        new SynchronousQueue<Runnable>(),
                                        threadFactory);
      }
  
+     /**
+      * Creates an Executor that starts a new Thread for each task.
+      * The number of threads created by the Executor is unbounded.
+      *
+      * <p> Invoking {@link Future#cancel(boolean) cancel(true)} on a {@link
+      * Future Future} representing the pending result of a task submitted to
+      * the Executor will {@link Thread#interrupt() interrupt} the thread
+      * executing the task.
+      *
+      * @param threadFactory the factory to use when creating new threads
+      * @return a new executor that creates a new Thread for each task
+      * @throws NullPointerException if threadFactory is null
+      * @since 99
+      */
+     @PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)
+     public static ExecutorService newThreadPerTaskExecutor(ThreadFactory threadFactory) {
+         return new ThreadPerTaskExecutor(threadFactory);
+     }
+ 
+     /**
+      * Creates an Executor that starts a new virtual Thread for each task.
+      * The number of threads created by the Executor is unbounded.
+      *
+      * <p> This method is equivalent to invoking
+      * {@link #newThreadPerTaskExecutor(ThreadFactory)} with a thread factory
+      * that creates virtual threads.
+      *
+      * @return a new executor that creates a new virtual Thread for each task
+      * @since 99
+      */
+     @PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)
+     public static ExecutorService newVirtualThreadPerTaskExecutor() {
+         ThreadFactory factory = Thread.ofVirtual().factory();
+         return newThreadPerTaskExecutor(factory);
+     }
+ 
      /**
       * Creates a single-threaded executor that can schedule commands
       * to run after a given delay, or to execute periodically.
       * (Note however that if this single
       * thread terminates due to a failure during execution prior to
< prev index next >