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

Print this page




 128  * <p>
 129  * Unless otherwise noted, passing a {@code null} argument to a constructor
 130  * or method in this class will cause a {@link NullPointerException} to be
 131  * thrown.
 132  *
 133  * @author  unascribed
 134  * @see     Runnable
 135  * @see     Runtime#exit(int)
 136  * @see     #run()
 137  * @see     #stop()
 138  * @since   JDK1.0
 139  */
 140 public
 141 class Thread implements Runnable {
 142     /* Make sure registerNatives is the first thing <clinit> does. */
 143     private static native void registerNatives();
 144     static {
 145         registerNatives();
 146     }
 147 
 148     private char        name[];
 149     private int         priority;
 150     private Thread      threadQ;
 151     private long        eetop;
 152 
 153     /* Whether or not to single_step this thread. */
 154     private boolean     single_step;
 155 
 156     /* Whether or not the thread is a daemon thread. */
 157     private boolean     daemon = false;
 158 
 159     /* JVM state */
 160     private boolean     stillborn = false;
 161 
 162     /* What will be run. */
 163     private Runnable target;
 164 
 165     /* The group of this thread */
 166     private ThreadGroup group;
 167 
 168     /* The context ClassLoader for this thread */


1118      * @see     #setPriority
1119      */
1120     public final int getPriority() {
1121         return priority;
1122     }
1123 
1124     /**
1125      * Changes the name of this thread to be equal to the argument
1126      * <code>name</code>.
1127      * <p>
1128      * First the <code>checkAccess</code> method of this thread is called
1129      * with no arguments. This may result in throwing a
1130      * <code>SecurityException</code>.
1131      *
1132      * @param      name   the new name for this thread.
1133      * @exception  SecurityException  if the current thread cannot modify this
1134      *               thread.
1135      * @see        #getName
1136      * @see        #checkAccess()
1137      */
1138     public final void setName(String name) {
1139         checkAccess();
1140         this.name = name.toCharArray();
1141         if (threadStatus != 0) {
1142             setNativeName(name);
1143         }
1144     }
1145 
1146     /**
1147      * Returns this thread's name.
1148      *
1149      * @return  this thread's name.
1150      * @see     #setName(String)
1151      */
1152     public final String getName() {
1153         return String.valueOf(name);
1154     }
1155 
1156     /**
1157      * Returns the thread group to which this thread belongs.
1158      * This method returns null if this thread has died
1159      * (been stopped).
1160      *
1161      * @return  this thread's thread group.
1162      */
1163     public final ThreadGroup getThreadGroup() {
1164         return group;
1165     }
1166 
1167     /**
1168      * Returns an estimate of the number of active threads in the current
1169      * thread's {@linkplain java.lang.ThreadGroup thread group} and its
1170      * subgroups. Recursively iterates over all subgroups in the current
1171      * thread's thread group.
1172      *
1173      * <p> The value returned is only an estimate because the number of




 128  * <p>
 129  * Unless otherwise noted, passing a {@code null} argument to a constructor
 130  * or method in this class will cause a {@link NullPointerException} to be
 131  * thrown.
 132  *
 133  * @author  unascribed
 134  * @see     Runnable
 135  * @see     Runtime#exit(int)
 136  * @see     #run()
 137  * @see     #stop()
 138  * @since   JDK1.0
 139  */
 140 public
 141 class Thread implements Runnable {
 142     /* Make sure registerNatives is the first thing <clinit> does. */
 143     private static native void registerNatives();
 144     static {
 145         registerNatives();
 146     }
 147 
 148     private volatile char  name[];
 149     private int            priority;
 150     private Thread         threadQ;
 151     private long           eetop;
 152 
 153     /* Whether or not to single_step this thread. */
 154     private boolean     single_step;
 155 
 156     /* Whether or not the thread is a daemon thread. */
 157     private boolean     daemon = false;
 158 
 159     /* JVM state */
 160     private boolean     stillborn = false;
 161 
 162     /* What will be run. */
 163     private Runnable target;
 164 
 165     /* The group of this thread */
 166     private ThreadGroup group;
 167 
 168     /* The context ClassLoader for this thread */


1118      * @see     #setPriority
1119      */
1120     public final int getPriority() {
1121         return priority;
1122     }
1123 
1124     /**
1125      * Changes the name of this thread to be equal to the argument
1126      * <code>name</code>.
1127      * <p>
1128      * First the <code>checkAccess</code> method of this thread is called
1129      * with no arguments. This may result in throwing a
1130      * <code>SecurityException</code>.
1131      *
1132      * @param      name   the new name for this thread.
1133      * @exception  SecurityException  if the current thread cannot modify this
1134      *               thread.
1135      * @see        #getName
1136      * @see        #checkAccess()
1137      */
1138     public final synchronized void setName(String name) {
1139         checkAccess();
1140         this.name = name.toCharArray();
1141         if (threadStatus != 0) {
1142             setNativeName(name);
1143         }
1144     }
1145 
1146     /**
1147      * Returns this thread's name.
1148      *
1149      * @return  this thread's name.
1150      * @see     #setName(String)
1151      */
1152     public final String getName() {
1153         return new String(name, true);
1154     }
1155 
1156     /**
1157      * Returns the thread group to which this thread belongs.
1158      * This method returns null if this thread has died
1159      * (been stopped).
1160      *
1161      * @return  this thread's thread group.
1162      */
1163     public final ThreadGroup getThreadGroup() {
1164         return group;
1165     }
1166 
1167     /**
1168      * Returns an estimate of the number of active threads in the current
1169      * thread's {@linkplain java.lang.ThreadGroup thread group} and its
1170      * subgroups. Recursively iterates over all subgroups in the current
1171      * thread's thread group.
1172      *
1173      * <p> The value returned is only an estimate because the number of