src/java.base/share/classes/java/lang/Thread.java
changeset 49491 f7363de371c9
parent 49490 1bc4cd2c5f8b
child 49925 3deb300f0e55
equal deleted inserted replaced
49490:1bc4cd2c5f8b 49491:f7363de371c9
   186     /*
   186     /*
   187      * The requested stack size for this thread, or 0 if the creator did
   187      * The requested stack size for this thread, or 0 if the creator did
   188      * not specify a stack size.  It is up to the VM to do whatever it
   188      * not specify a stack size.  It is up to the VM to do whatever it
   189      * likes with this number; some VMs will ignore it.
   189      * likes with this number; some VMs will ignore it.
   190      */
   190      */
   191     private long stackSize;
   191     private final long stackSize;
   192 
   192 
   193     /*
   193     /*
   194      * JVM-private state that persists after native thread termination.
   194      * JVM-private state that persists after native thread termination.
   195      */
   195      */
   196     private long nativeParkEventPointer;
   196     private long nativeParkEventPointer;
   197 
   197 
   198     /*
   198     /*
   199      * Thread ID
   199      * Thread ID
   200      */
   200      */
   201     private long tid;
   201     private final long tid;
   202 
   202 
   203     /* For generating thread ID */
   203     /* For generating thread ID */
   204     private static long threadSeqNumber;
   204     private static long threadSeqNumber;
   205 
   205 
       
   206     private static synchronized long nextThreadID() {
       
   207         return ++threadSeqNumber;
       
   208     }
       
   209 
   206     /*
   210     /*
   207      * Java thread status for tools, default indicates thread 'not yet started'
   211      * Java thread status for tools, default indicates thread 'not yet started'
   208      */
   212      */
   209     private volatile int threadStatus;
   213     private volatile int threadStatus;
   210 
       
   211     private static synchronized long nextThreadID() {
       
   212         return ++threadSeqNumber;
       
   213     }
       
   214 
   214 
   215     /**
   215     /**
   216      * The argument supplied to the current call to
   216      * The argument supplied to the current call to
   217      * java.util.concurrent.locks.LockSupport.park.
   217      * java.util.concurrent.locks.LockSupport.park.
   218      * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
   218      * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
   375      */
   375      */
   376     @HotSpotIntrinsicCandidate
   376     @HotSpotIntrinsicCandidate
   377     public static void onSpinWait() {}
   377     public static void onSpinWait() {}
   378 
   378 
   379     /**
   379     /**
   380      * Initializes a Thread with the current AccessControlContext.
       
   381      * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean)
       
   382      */
       
   383     private void init(ThreadGroup g, Runnable target, String name,
       
   384                       long stackSize) {
       
   385         init(g, target, name, stackSize, null, true);
       
   386     }
       
   387 
       
   388     /**
       
   389      * Initializes a Thread.
   380      * Initializes a Thread.
   390      *
   381      *
   391      * @param g the Thread group
   382      * @param g the Thread group
   392      * @param target the object whose run() method gets called
   383      * @param target the object whose run() method gets called
   393      * @param name the name of the new Thread
   384      * @param name the name of the new Thread
   396      * @param acc the AccessControlContext to inherit, or
   387      * @param acc the AccessControlContext to inherit, or
   397      *            AccessController.getContext() if null
   388      *            AccessController.getContext() if null
   398      * @param inheritThreadLocals if {@code true}, inherit initial values for
   389      * @param inheritThreadLocals if {@code true}, inherit initial values for
   399      *            inheritable thread-locals from the constructing thread
   390      *            inheritable thread-locals from the constructing thread
   400      */
   391      */
   401     private void init(ThreadGroup g, Runnable target, String name,
   392     private Thread(ThreadGroup g, Runnable target, String name,
   402                       long stackSize, AccessControlContext acc,
   393                    long stackSize, AccessControlContext acc,
   403                       boolean inheritThreadLocals) {
   394                    boolean inheritThreadLocals) {
   404         if (name == null) {
   395         if (name == null) {
   405             throw new NullPointerException("name cannot be null");
   396             throw new NullPointerException("name cannot be null");
   406         }
   397         }
   407 
   398 
   408         this.name = name;
   399         this.name = name;
   416                what to do. */
   407                what to do. */
   417             if (security != null) {
   408             if (security != null) {
   418                 g = security.getThreadGroup();
   409                 g = security.getThreadGroup();
   419             }
   410             }
   420 
   411 
   421             /* If the security doesn't have a strong opinion of the matter
   412             /* If the security manager doesn't have a strong opinion
   422                use the parent thread group. */
   413                on the matter, use the parent thread group. */
   423             if (g == null) {
   414             if (g == null) {
   424                 g = parent.getThreadGroup();
   415                 g = parent.getThreadGroup();
   425             }
   416             }
   426         }
   417         }
   427 
   418 
   456                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
   447                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
   457         /* Stash the specified stack size in case the VM cares */
   448         /* Stash the specified stack size in case the VM cares */
   458         this.stackSize = stackSize;
   449         this.stackSize = stackSize;
   459 
   450 
   460         /* Set thread ID */
   451         /* Set thread ID */
   461         tid = nextThreadID();
   452         this.tid = nextThreadID();
   462     }
   453     }
   463 
   454 
   464     /**
   455     /**
   465      * Throws CloneNotSupportedException as a Thread can not be meaningfully
   456      * Throws CloneNotSupportedException as a Thread can not be meaningfully
   466      * cloned. Construct a new Thread instead.
   457      * cloned. Construct a new Thread instead.
   479      * {@code (null, null, gname)}, where {@code gname} is a newly generated
   470      * {@code (null, null, gname)}, where {@code gname} is a newly generated
   480      * name. Automatically generated names are of the form
   471      * name. Automatically generated names are of the form
   481      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
   472      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
   482      */
   473      */
   483     public Thread() {
   474     public Thread() {
   484         init(null, null, "Thread-" + nextThreadNum(), 0);
   475         this(null, null, "Thread-" + nextThreadNum(), 0);
   485     }
   476     }
   486 
   477 
   487     /**
   478     /**
   488      * Allocates a new {@code Thread} object. This constructor has the same
   479      * Allocates a new {@code Thread} object. This constructor has the same
   489      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   480      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   495      *         the object whose {@code run} method is invoked when this thread
   486      *         the object whose {@code run} method is invoked when this thread
   496      *         is started. If {@code null}, this classes {@code run} method does
   487      *         is started. If {@code null}, this classes {@code run} method does
   497      *         nothing.
   488      *         nothing.
   498      */
   489      */
   499     public Thread(Runnable target) {
   490     public Thread(Runnable target) {
   500         init(null, target, "Thread-" + nextThreadNum(), 0);
   491         this(null, target, "Thread-" + nextThreadNum(), 0);
   501     }
   492     }
   502 
   493 
   503     /**
   494     /**
   504      * Creates a new Thread that inherits the given AccessControlContext
   495      * Creates a new Thread that inherits the given AccessControlContext
   505      * but thread-local variables are not inherited.
   496      * but thread-local variables are not inherited.
   506      * This is not a public constructor.
   497      * This is not a public constructor.
   507      */
   498      */
   508     Thread(Runnable target, AccessControlContext acc) {
   499     Thread(Runnable target, AccessControlContext acc) {
   509         init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
   500         this(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
   510     }
   501     }
   511 
   502 
   512     /**
   503     /**
   513      * Allocates a new {@code Thread} object. This constructor has the same
   504      * Allocates a new {@code Thread} object. This constructor has the same
   514      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   505      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   531      * @throws  SecurityException
   522      * @throws  SecurityException
   532      *          if the current thread cannot create a thread in the specified
   523      *          if the current thread cannot create a thread in the specified
   533      *          thread group
   524      *          thread group
   534      */
   525      */
   535     public Thread(ThreadGroup group, Runnable target) {
   526     public Thread(ThreadGroup group, Runnable target) {
   536         init(group, target, "Thread-" + nextThreadNum(), 0);
   527         this(group, target, "Thread-" + nextThreadNum(), 0);
   537     }
   528     }
   538 
   529 
   539     /**
   530     /**
   540      * Allocates a new {@code Thread} object. This constructor has the same
   531      * Allocates a new {@code Thread} object. This constructor has the same
   541      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   532      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   543      *
   534      *
   544      * @param   name
   535      * @param   name
   545      *          the name of the new thread
   536      *          the name of the new thread
   546      */
   537      */
   547     public Thread(String name) {
   538     public Thread(String name) {
   548         init(null, null, name, 0);
   539         this(null, null, name, 0);
   549     }
   540     }
   550 
   541 
   551     /**
   542     /**
   552      * Allocates a new {@code Thread} object. This constructor has the same
   543      * Allocates a new {@code Thread} object. This constructor has the same
   553      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   544      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   567      * @throws  SecurityException
   558      * @throws  SecurityException
   568      *          if the current thread cannot create a thread in the specified
   559      *          if the current thread cannot create a thread in the specified
   569      *          thread group
   560      *          thread group
   570      */
   561      */
   571     public Thread(ThreadGroup group, String name) {
   562     public Thread(ThreadGroup group, String name) {
   572         init(group, null, name, 0);
   563         this(group, null, name, 0);
   573     }
   564     }
   574 
   565 
   575     /**
   566     /**
   576      * Allocates a new {@code Thread} object. This constructor has the same
   567      * Allocates a new {@code Thread} object. This constructor has the same
   577      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   568      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
   583      *
   574      *
   584      * @param  name
   575      * @param  name
   585      *         the name of the new thread
   576      *         the name of the new thread
   586      */
   577      */
   587     public Thread(Runnable target, String name) {
   578     public Thread(Runnable target, String name) {
   588         init(null, target, name, 0);
   579         this(null, target, name, 0);
   589     }
   580     }
   590 
   581 
   591     /**
   582     /**
   592      * Allocates a new {@code Thread} object so that it has {@code target}
   583      * Allocates a new {@code Thread} object so that it has {@code target}
   593      * as its run object, has the specified {@code name} as its name,
   584      * as its run object, has the specified {@code name} as its name,
   631      * @throws  SecurityException
   622      * @throws  SecurityException
   632      *          if the current thread cannot create a thread in the specified
   623      *          if the current thread cannot create a thread in the specified
   633      *          thread group or cannot override the context class loader methods.
   624      *          thread group or cannot override the context class loader methods.
   634      */
   625      */
   635     public Thread(ThreadGroup group, Runnable target, String name) {
   626     public Thread(ThreadGroup group, Runnable target, String name) {
   636         init(group, target, name, 0);
   627         this(group, target, name, 0);
   637     }
   628     }
   638 
   629 
   639     /**
   630     /**
   640      * Allocates a new {@code Thread} object so that it has {@code target}
   631      * Allocates a new {@code Thread} object so that it has {@code target}
   641      * as its run object, has the specified {@code name} as its name,
   632      * as its run object, has the specified {@code name} as its name,
   710      *
   701      *
   711      * @since 1.4
   702      * @since 1.4
   712      */
   703      */
   713     public Thread(ThreadGroup group, Runnable target, String name,
   704     public Thread(ThreadGroup group, Runnable target, String name,
   714                   long stackSize) {
   705                   long stackSize) {
   715         init(group, target, name, stackSize);
   706         this(group, target, name, stackSize, null, true);
   716     }
   707     }
   717 
   708 
   718     /**
   709     /**
   719      * Allocates a new {@code Thread} object so that it has {@code target}
   710      * Allocates a new {@code Thread} object so that it has {@code target}
   720      * as its run object, has the specified {@code name} as its name,
   711      * as its run object, has the specified {@code name} as its name,
   766      *
   757      *
   767      * @since 9
   758      * @since 9
   768      */
   759      */
   769     public Thread(ThreadGroup group, Runnable target, String name,
   760     public Thread(ThreadGroup group, Runnable target, String name,
   770                   long stackSize, boolean inheritThreadLocals) {
   761                   long stackSize, boolean inheritThreadLocals) {
   771         init(group, target, name, stackSize, null, inheritThreadLocals);
   762         this(group, target, name, stackSize, null, inheritThreadLocals);
   772     }
   763     }
   773 
   764 
   774     /**
   765     /**
   775      * Causes this thread to begin execution; the Java Virtual Machine
   766      * Causes this thread to begin execution; the Java Virtual Machine
   776      * calls the {@code run} method of this thread.
   767      * calls the {@code run} method of this thread.