< prev index next >

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

Print this page
*** 22,11 ***
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  package java.lang;
  
- import java.lang.Thread.Builder;
  import java.lang.Thread.Builder.OfPlatform;
  import java.lang.Thread.Builder.OfVirtual;
  import java.lang.Thread.UncaughtExceptionHandler;
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.VarHandle;
--- 22,10 ---

*** 39,16 ***
  
  /**
   * Defines static methods to create platform and virtual thread builders.
   */
  class ThreadBuilders {
  
      /**
!      * Base implementation of ThreadBuilder.
       */
!     static abstract non-sealed
-     class BaseThreadBuilder<T extends Builder> implements Builder {
          private String name;
          private long counter;
          private int characteristics;
          private UncaughtExceptionHandler uhe;
  
--- 38,16 ---
  
  /**
   * Defines static methods to create platform and virtual thread builders.
   */
  class ThreadBuilders {
+     private ThreadBuilders() { }
  
      /**
!      * Base class for Thread.Builder implementations.
       */
!     private static class BaseThreadBuilder {
          private String name;
          private long counter;
          private int characteristics;
          private UncaughtExceptionHandler uhe;
  

*** 74,64 ***
              } else {
                  return name;
              }
          }
  
!         @Override
-         @SuppressWarnings("unchecked")
-         public T name(String name) {
              this.name = Objects.requireNonNull(name);
              this.counter = -1;
-             return (T) this;
          }
  
!         @Override
-         @SuppressWarnings("unchecked")
-         public T name(String prefix, long start) {
              Objects.requireNonNull(prefix);
              if (start < 0)
                  throw new IllegalArgumentException("'start' is negative");
              this.name = prefix;
              this.counter = start;
-             return (T) this;
          }
  
!         @Override
-         @SuppressWarnings("unchecked")
-         public T allowSetThreadLocals(boolean allow) {
-             if (allow) {
-                 characteristics &= ~Thread.NO_THREAD_LOCALS;
-             } else {
-                 characteristics |= Thread.NO_THREAD_LOCALS;
-             }
-             return (T) this;
-         }
- 
-         @Override
-         @SuppressWarnings("unchecked")
-         public T inheritInheritableThreadLocals(boolean inherit) {
              if (inherit) {
                  characteristics &= ~Thread.NO_INHERIT_THREAD_LOCALS;
              } else {
                  characteristics |= Thread.NO_INHERIT_THREAD_LOCALS;
              }
-             return (T) this;
          }
  
!         @Override
-         @SuppressWarnings("unchecked")
-         public T uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
              this.uhe = Objects.requireNonNull(ueh);
-             return (T) this;
          }
      }
  
      /**
       * ThreadBuilder.OfPlatform implementation.
       */
      static final class PlatformThreadBuilder
!             extends BaseThreadBuilder<OfPlatform> implements OfPlatform {
          private ThreadGroup group;
          private boolean daemon;
          private boolean daemonChanged;
          private int priority;
          private long stackSize;
--- 73,41 ---
              } else {
                  return name;
              }
          }
  
!         void setName(String name) {
              this.name = Objects.requireNonNull(name);
              this.counter = -1;
          }
  
!         void setName(String prefix, long start) {
              Objects.requireNonNull(prefix);
              if (start < 0)
                  throw new IllegalArgumentException("'start' is negative");
              this.name = prefix;
              this.counter = start;
          }
  
!         void setInheritInheritableThreadLocals(boolean inherit) {
              if (inherit) {
                  characteristics &= ~Thread.NO_INHERIT_THREAD_LOCALS;
              } else {
                  characteristics |= Thread.NO_INHERIT_THREAD_LOCALS;
              }
          }
  
!         void setUncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
              this.uhe = Objects.requireNonNull(ueh);
          }
      }
  
      /**
       * ThreadBuilder.OfPlatform implementation.
       */
      static final class PlatformThreadBuilder
!             extends BaseThreadBuilder implements OfPlatform {
          private ThreadGroup group;
          private boolean daemon;
          private boolean daemonChanged;
          private int priority;
          private long stackSize;

*** 143,10 ***
--- 119,34 ---
          String nextThreadName() {
              String name = super.nextThreadName();
              return (name != null) ? name : Thread.genThreadName();
          }
  
+         @Override
+         public OfPlatform name(String name) {
+             setName(name);
+             return this;
+         }
+ 
+         @Override
+         public OfPlatform name(String prefix, long start) {
+             setName(prefix, start);
+             return this;
+         }
+ 
+         @Override
+         public OfPlatform inheritInheritableThreadLocals(boolean inherit) {
+             setInheritInheritableThreadLocals(inherit);
+             return this;
+         }
+ 
+         @Override
+         public OfPlatform uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
+             setUncaughtExceptionHandler(ueh);
+             return this;
+         }
+ 
          @Override
          public OfPlatform group(ThreadGroup group) {
              this.group = Objects.requireNonNull(group);
              return this;
          }

*** 206,11 ***
  
      /**
       * ThreadBuilder.OfVirtual implementation.
       */
      static final class VirtualThreadBuilder
!             extends BaseThreadBuilder<OfVirtual> implements OfVirtual {
          private Executor scheduler;
  
          VirtualThreadBuilder() {
          }
  
--- 206,11 ---
  
      /**
       * ThreadBuilder.OfVirtual implementation.
       */
      static final class VirtualThreadBuilder
!             extends BaseThreadBuilder implements OfVirtual {
          private Executor scheduler;
  
          VirtualThreadBuilder() {
          }
  

*** 219,10 ***
--- 219,34 ---
              if (!ContinuationSupport.isSupported())
                  throw new UnsupportedOperationException();
              this.scheduler = Objects.requireNonNull(scheduler);
          }
  
+         @Override
+         public OfVirtual name(String name) {
+             setName(name);
+             return this;
+         }
+ 
+         @Override
+         public OfVirtual name(String prefix, long start) {
+             setName(prefix, start);
+             return this;
+         }
+ 
+         @Override
+         public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
+             setInheritInheritableThreadLocals(inherit);
+             return this;
+         }
+ 
+         @Override
+         public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
+             setUncaughtExceptionHandler(ueh);
+             return this;
+         }
+ 
          @Override
          public Thread unstarted(Runnable task) {
              Objects.requireNonNull(task);
              var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
              UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
< prev index next >