8200123: Replace Thread.init with telescoping constructor jdk-11+7
authormartin
Thu, 29 Mar 2018 16:23:35 -0700
changeset 49491 f7363de371c9
parent 49490 1bc4cd2c5f8b
child 49492 f1a8ec1a6972
8200123: Replace Thread.init with telescoping constructor Reviewed-by: dholmes, mchung, plevart
src/java.base/share/classes/java/lang/Thread.java
src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java
--- a/src/java.base/share/classes/java/lang/Thread.java	Thu Mar 29 16:21:54 2018 -0700
+++ b/src/java.base/share/classes/java/lang/Thread.java	Thu Mar 29 16:23:35 2018 -0700
@@ -188,7 +188,7 @@
      * 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.
@@ -198,20 +198,20 @@
     /*
      * 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.
@@ -377,15 +377,6 @@
     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
@@ -398,9 +389,9 @@
      * @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,
-                      long stackSize, AccessControlContext acc,
-                      boolean inheritThreadLocals) {
+    private Thread(ThreadGroup g, Runnable target, String name,
+                   long stackSize, AccessControlContext acc,
+                   boolean inheritThreadLocals) {
         if (name == null) {
             throw new NullPointerException("name cannot be null");
         }
@@ -418,8 +409,8 @@
                 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();
             }
@@ -458,7 +449,7 @@
         this.stackSize = stackSize;
 
         /* Set thread ID */
-        tid = nextThreadID();
+        this.tid = nextThreadID();
     }
 
     /**
@@ -481,7 +472,7 @@
      * {@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);
     }
 
     /**
@@ -497,7 +488,7 @@
      *         nothing.
      */
     public Thread(Runnable target) {
-        init(null, target, "Thread-" + nextThreadNum(), 0);
+        this(null, target, "Thread-" + nextThreadNum(), 0);
     }
 
     /**
@@ -506,7 +497,7 @@
      * 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);
     }
 
     /**
@@ -533,7 +524,7 @@
      *          thread group
      */
     public Thread(ThreadGroup group, Runnable target) {
-        init(group, target, "Thread-" + nextThreadNum(), 0);
+        this(group, target, "Thread-" + nextThreadNum(), 0);
     }
 
     /**
@@ -545,7 +536,7 @@
      *          the name of the new thread
      */
     public Thread(String name) {
-        init(null, null, name, 0);
+        this(null, null, name, 0);
     }
 
     /**
@@ -569,7 +560,7 @@
      *          thread group
      */
     public Thread(ThreadGroup group, String name) {
-        init(group, null, name, 0);
+        this(group, null, name, 0);
     }
 
     /**
@@ -585,7 +576,7 @@
      *         the name of the new thread
      */
     public Thread(Runnable target, String name) {
-        init(null, target, name, 0);
+        this(null, target, name, 0);
     }
 
     /**
@@ -633,7 +624,7 @@
      *          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);
     }
 
     /**
@@ -712,7 +703,7 @@
      */
     public Thread(ThreadGroup group, Runnable target, String name,
                   long stackSize) {
-        init(group, target, name, stackSize);
+        this(group, target, name, stackSize, null, true);
     }
 
     /**
@@ -768,7 +759,7 @@
      */
     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);
     }
 
     /**
--- a/src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java	Thu Mar 29 16:21:54 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java	Thu Mar 29 16:23:35 2018 -0700
@@ -412,11 +412,11 @@
     /**
      * Returns the thread id for the given thread.  We must access
      * this directly rather than via method Thread.getId() because
-     * getId() is not final, and has been known to be overridden in
-     * ways that do not preserve unique mappings.
+     * getId() has been known to be overridden in ways that do not
+     * preserve unique mappings.
      */
     static final long getThreadId(Thread thread) {
-        return U.getLongVolatile(thread, TID);
+        return U.getLong(thread, TID);
     }
 
     // Hotspot implementation via intrinsics API