< prev index next >

src/java.base/share/classes/java/lang/ThreadBuilders.java

Print this page

 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.lang.Thread.Builder.OfPlatform;
 28 import java.lang.Thread.Builder.OfVirtual;
 29 import java.lang.Thread.UncaughtExceptionHandler;
 30 import java.lang.invoke.MethodHandles;
 31 import java.lang.invoke.VarHandle;
 32 import java.util.Locale;
 33 import java.util.Objects;
 34 import java.util.concurrent.Executor;
 35 import java.util.concurrent.ThreadFactory;
 36 import jdk.internal.misc.Unsafe;
 37 import jdk.internal.invoke.MhUtil;


 38 import jdk.internal.vm.ContinuationSupport;
 39 
 40 /**
 41  * Defines static methods to create platform and virtual thread builders.
 42  */
 43 class ThreadBuilders {
 44     private ThreadBuilders() { }
 45 
 46     /**
 47      * Base class for Thread.Builder implementations.
 48      */
 49     private static class BaseThreadBuilder {
 50         private String name;
 51         private long counter;
 52         private int characteristics;
 53         private UncaughtExceptionHandler uhe;
 54 
 55         String name() {
 56             return name;
 57         }

198         }
199 
200         @Override
201         public ThreadFactory factory() {
202             return new PlatformThreadFactory(group, name(), counter(), characteristics(),
203                     daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
204         }
205 
206     }
207 
208     /**
209      * ThreadBuilder.OfVirtual implementation.
210      */
211     static final class VirtualThreadBuilder
212             extends BaseThreadBuilder implements OfVirtual {
213         private Executor scheduler;
214 
215         VirtualThreadBuilder() {
216         }
217 
218         // invoked by tests
219         VirtualThreadBuilder(Executor scheduler) {
220             if (!ContinuationSupport.isSupported())
221                 throw new UnsupportedOperationException();
222             this.scheduler = Objects.requireNonNull(scheduler);
223         }
224 
225         @Override
226         public OfVirtual name(String name) {
227             setName(name);
228             return this;
229         }
230 
231         @Override
232         public OfVirtual name(String prefix, long start) {
233             setName(prefix, start);
234             return this;
235         }
236 
237         @Override
238         public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
239             setInheritInheritableThreadLocals(inherit);
240             return this;
241         }
242 
243         @Override
244         public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {

251             Objects.requireNonNull(task);
252             var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
253             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
254             if (uhe != null)
255                 thread.uncaughtExceptionHandler(uhe);
256             return thread;
257         }
258 
259         @Override
260         public Thread start(Runnable task) {
261             Thread thread = unstarted(task);
262             thread.start();
263             return thread;
264         }
265 
266         @Override
267         public ThreadFactory factory() {
268             return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
269                     uncaughtExceptionHandler());
270         }












271     }
272 
273     /**
274      * Base ThreadFactory implementation.
275      */
276     private abstract static class BaseThreadFactory implements ThreadFactory {
277         private static final VarHandle COUNT = MhUtil.findVarHandle(
278                 MethodHandles.lookup(), "count", long.class);
279 
280         private final String name;
281         private final int characteristics;
282         private final UncaughtExceptionHandler uhe;
283 
284         private final boolean hasCounter;
285         private volatile long count;
286 
287         BaseThreadFactory(String name,
288                           long start,
289                           int characteristics,
290                           UncaughtExceptionHandler uhe)  {

 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.lang.Thread.Builder.OfPlatform;
 28 import java.lang.Thread.Builder.OfVirtual;
 29 import java.lang.Thread.UncaughtExceptionHandler;
 30 import java.lang.invoke.MethodHandles;
 31 import java.lang.invoke.VarHandle;
 32 import java.util.Locale;
 33 import java.util.Objects;
 34 import java.util.concurrent.Executor;
 35 import java.util.concurrent.ThreadFactory;
 36 import jdk.internal.misc.Unsafe;
 37 import jdk.internal.invoke.MhUtil;
 38 import jdk.internal.reflect.CallerSensitive;
 39 import jdk.internal.reflect.Reflection;
 40 import jdk.internal.vm.ContinuationSupport;
 41 
 42 /**
 43  * Defines static methods to create platform and virtual thread builders.
 44  */
 45 class ThreadBuilders {
 46     private ThreadBuilders() { }
 47 
 48     /**
 49      * Base class for Thread.Builder implementations.
 50      */
 51     private static class BaseThreadBuilder {
 52         private String name;
 53         private long counter;
 54         private int characteristics;
 55         private UncaughtExceptionHandler uhe;
 56 
 57         String name() {
 58             return name;
 59         }

200         }
201 
202         @Override
203         public ThreadFactory factory() {
204             return new PlatformThreadFactory(group, name(), counter(), characteristics(),
205                     daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
206         }
207 
208     }
209 
210     /**
211      * ThreadBuilder.OfVirtual implementation.
212      */
213     static final class VirtualThreadBuilder
214             extends BaseThreadBuilder implements OfVirtual {
215         private Executor scheduler;
216 
217         VirtualThreadBuilder() {
218         }
219 







220         @Override
221         public OfVirtual name(String name) {
222             setName(name);
223             return this;
224         }
225 
226         @Override
227         public OfVirtual name(String prefix, long start) {
228             setName(prefix, start);
229             return this;
230         }
231 
232         @Override
233         public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
234             setInheritInheritableThreadLocals(inherit);
235             return this;
236         }
237 
238         @Override
239         public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {

246             Objects.requireNonNull(task);
247             var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
248             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
249             if (uhe != null)
250                 thread.uncaughtExceptionHandler(uhe);
251             return thread;
252         }
253 
254         @Override
255         public Thread start(Runnable task) {
256             Thread thread = unstarted(task);
257             thread.start();
258             return thread;
259         }
260 
261         @Override
262         public ThreadFactory factory() {
263             return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
264                     uncaughtExceptionHandler());
265         }
266 
267         @CallerSensitive
268         @Override
269         public OfVirtual scheduler(Executor scheduler) {
270             Class<?> caller = Reflection.getCallerClass();
271             caller.getModule().ensureNativeAccess(OfVirtual.class, "scheduler", caller, false);
272             if (!ContinuationSupport.isSupported()) {
273                 throw new UnsupportedOperationException();
274             }
275             this.scheduler = Objects.requireNonNull(scheduler);
276             return this;
277         }
278     }
279 
280     /**
281      * Base ThreadFactory implementation.
282      */
283     private abstract static class BaseThreadFactory implements ThreadFactory {
284         private static final VarHandle COUNT = MhUtil.findVarHandle(
285                 MethodHandles.lookup(), "count", long.class);
286 
287         private final String name;
288         private final int characteristics;
289         private final UncaughtExceptionHandler uhe;
290 
291         private final boolean hasCounter;
292         private volatile long count;
293 
294         BaseThreadFactory(String name,
295                           long start,
296                           int characteristics,
297                           UncaughtExceptionHandler uhe)  {
< prev index next >