< prev index next >

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

Print this page
8200123: Replace Thread.init with telescoping constructor
Reviewed-by: dholmes, mchung, plevart

@@ -186,34 +186,34 @@
     /*
      * The requested stack size for this thread, or 0 if the creator did
      * not specify a stack size.  It is up to the VM to do whatever it
      * likes with this number; some VMs will ignore it.
      */
-    private long stackSize;
+    private final long stackSize;
 
     /*
      * JVM-private state that persists after native thread termination.
      */
     private long nativeParkEventPointer;
 
     /*
      * Thread ID
      */
-    private long tid;
+    private final long tid;
 
     /* For generating thread ID */
     private static long threadSeqNumber;
 
+    private static synchronized long nextThreadID() {
+        return ++threadSeqNumber;
+    }
+
     /*
      * Java thread status for tools, default indicates thread 'not yet started'
      */
     private volatile int threadStatus;
 
-    private static synchronized long nextThreadID() {
-        return ++threadSeqNumber;
-    }
-
     /**
      * The argument supplied to the current call to
      * java.util.concurrent.locks.LockSupport.park.
      * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
      * Accessed using java.util.concurrent.locks.LockSupport.getBlocker

@@ -375,19 +375,10 @@
      */
     @HotSpotIntrinsicCandidate
     public static void onSpinWait() {}
 
     /**
-     * Initializes a Thread with the current AccessControlContext.
-     * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean)
-     */
-    private void init(ThreadGroup g, Runnable target, String name,
-                      long stackSize) {
-        init(g, target, name, stackSize, null, true);
-    }
-
-    /**
      * Initializes a Thread.
      *
      * @param g the Thread group
      * @param target the object whose run() method gets called
      * @param name the name of the new Thread

@@ -396,11 +387,11 @@
      * @param acc the AccessControlContext to inherit, or
      *            AccessController.getContext() if null
      * @param inheritThreadLocals if {@code true}, inherit initial values for
      *            inheritable thread-locals from the constructing thread
      */
-    private void init(ThreadGroup g, Runnable target, String name,
+    private Thread(ThreadGroup g, Runnable target, String name,
                       long stackSize, AccessControlContext acc,
                       boolean inheritThreadLocals) {
         if (name == null) {
             throw new NullPointerException("name cannot be null");
         }

@@ -416,12 +407,12 @@
                what to do. */
             if (security != null) {
                 g = security.getThreadGroup();
             }
 
-            /* If the security doesn't have a strong opinion of the matter
-               use the parent thread group. */
+            /* If the security manager doesn't have a strong opinion
+               on the matter, use the parent thread group. */
             if (g == null) {
                 g = parent.getThreadGroup();
             }
         }
 

@@ -456,11 +447,11 @@
                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
         /* Stash the specified stack size in case the VM cares */
         this.stackSize = stackSize;
 
         /* Set thread ID */
-        tid = nextThreadID();
+        this.tid = nextThreadID();
     }
 
     /**
      * Throws CloneNotSupportedException as a Thread can not be meaningfully
      * cloned. Construct a new Thread instead.

@@ -479,11 +470,11 @@
      * {@code (null, null, gname)}, where {@code gname} is a newly generated
      * name. Automatically generated names are of the form
      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
      */
     public Thread() {
-        init(null, null, "Thread-" + nextThreadNum(), 0);
+        this(null, null, "Thread-" + nextThreadNum(), 0);
     }
 
     /**
      * Allocates a new {@code Thread} object. This constructor has the same
      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}

@@ -495,20 +486,20 @@
      *         the object whose {@code run} method is invoked when this thread
      *         is started. If {@code null}, this classes {@code run} method does
      *         nothing.
      */
     public Thread(Runnable target) {
-        init(null, target, "Thread-" + nextThreadNum(), 0);
+        this(null, target, "Thread-" + nextThreadNum(), 0);
     }
 
     /**
      * Creates a new Thread that inherits the given AccessControlContext
      * but thread-local variables are not inherited.
      * This is not a public constructor.
      */
     Thread(Runnable target, AccessControlContext acc) {
-        init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
+        this(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
     }
 
     /**
      * Allocates a new {@code Thread} object. This constructor has the same
      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}

@@ -531,11 +522,11 @@
      * @throws  SecurityException
      *          if the current thread cannot create a thread in the specified
      *          thread group
      */
     public Thread(ThreadGroup group, Runnable target) {
-        init(group, target, "Thread-" + nextThreadNum(), 0);
+        this(group, target, "Thread-" + nextThreadNum(), 0);
     }
 
     /**
      * Allocates a new {@code Thread} object. This constructor has the same
      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}

@@ -543,11 +534,11 @@
      *
      * @param   name
      *          the name of the new thread
      */
     public Thread(String name) {
-        init(null, null, name, 0);
+        this(null, null, name, 0);
     }
 
     /**
      * Allocates a new {@code Thread} object. This constructor has the same
      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}

@@ -567,11 +558,11 @@
      * @throws  SecurityException
      *          if the current thread cannot create a thread in the specified
      *          thread group
      */
     public Thread(ThreadGroup group, String name) {
-        init(group, null, name, 0);
+        this(group, null, name, 0);
     }
 
     /**
      * Allocates a new {@code Thread} object. This constructor has the same
      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}

@@ -583,11 +574,11 @@
      *
      * @param  name
      *         the name of the new thread
      */
     public Thread(Runnable target, String name) {
-        init(null, target, name, 0);
+        this(null, target, name, 0);
     }
 
     /**
      * Allocates a new {@code Thread} object so that it has {@code target}
      * as its run object, has the specified {@code name} as its name,

@@ -631,11 +622,11 @@
      * @throws  SecurityException
      *          if the current thread cannot create a thread in the specified
      *          thread group or cannot override the context class loader methods.
      */
     public Thread(ThreadGroup group, Runnable target, String name) {
-        init(group, target, name, 0);
+        this(group, target, name, 0);
     }
 
     /**
      * Allocates a new {@code Thread} object so that it has {@code target}
      * as its run object, has the specified {@code name} as its name,

@@ -710,11 +701,11 @@
      *
      * @since 1.4
      */
     public Thread(ThreadGroup group, Runnable target, String name,
                   long stackSize) {
-        init(group, target, name, stackSize);
+        this(group, target, name, stackSize, null, true);
     }
 
     /**
      * Allocates a new {@code Thread} object so that it has {@code target}
      * as its run object, has the specified {@code name} as its name,

@@ -766,11 +757,11 @@
      *
      * @since 9
      */
     public Thread(ThreadGroup group, Runnable target, String name,
                   long stackSize, boolean inheritThreadLocals) {
-        init(group, target, name, stackSize, null, inheritThreadLocals);
+        this(group, target, name, stackSize, null, inheritThreadLocals);
     }
 
     /**
      * Causes this thread to begin execution; the Java Virtual Machine
      * calls the {@code run} method of this thread.
< prev index next >